[英]Expose .NET class functions without wrapping them
I have a COMVisible
class MyAPI that my clients are going to use as the root object for all API access.我有一个
COMVisible
类MyAPI ,我的客户将使用它作为所有 API 访问的根对象。
I have my APIs in another assembly OtherAPI that has Document and Window API classes (not COM visible).我的 API 位于另一个程序集OtherAPI 中,该程序集具有Document和Window API 类(COM 不可见)。
I want to expose Document and Window classes via MyAPI class to a late binding client such as JavaScript.我想通过MyAPI类向后期绑定客户端(如 JavaScript)公开Document和Window类。
[COMVisible(true)]
class MyAPI
{
public OtherAPI::Document NewDocument()
{
return new OtherAPI::Document();
}
}
// Something like this should work in the client code
(new MyAPI()).NewDocument().GetName();
The problem is that it does not see GetName because that is not COM visible
I think.问题是它没有看到GetName因为我认为那不是
COM visible
。
I can wrap each and every function from Document and Window class in this class but I am looking for an elegant solution.我可以将 Document 和 Window 类中的每个函数都包装在这个类中,但我正在寻找一个优雅的解决方案。 I want my clients app to be able to use both Document and Window class functions via MyAPI object.
我希望我的客户端应用程序能够通过 MyAPI 对象使用 Document 和 Window 类函数。
You need to read the documentation...您需要阅读文档...
Important parts highlighted for you viewing pleasure :)突出显示重要部分,让您观看愉快:)
ComVisibleAttribute Class ComVisibleAttribute 类
Setting the attribute to false on a specific type hides that type and its members.
将特定类型的属性设置为 false 会隐藏该类型及其成员。 However, you cannot make members of a type visible if the type is invisible.
但是,如果类型不可见,则不能使该类型的成员可见。 Setting the attribute to false on a type prevents that type from being exported to a type library;
将类型的属性设置为 false 会阻止该类型被导出到类型库; classes are not registered;
课程未注册; interfaces are never responsive to unmanaged QueryInterface calls.
接口永远不会响应非托管的 QueryInterface 调用。
However!然而!
Unless you explicitly set a class and its members to false, inherited classes can expose to COM base class members that are invisible in the original class .
除非您将类及其成员显式设置为 false,否则继承的类可以向原始类中不可见的COM 基类成员公开。 For example, if you set ClassA to false and do not apply the attribute to its members , the class and its members are invisible to COM.
例如,如果将ClassA设置为 false并且不将该特性应用于其 members ,则该类及其成员对 COM 不可见。 However, if you derive ClassB from ClassA and export ClassB to COM, ClassA members become visible base class members of ClassB.
但是,如果从ClassA派生ClassB并将ClassB导出到 COM,则ClassA成员将成为 ClassB 的可见基类成员。
So it reads very clearly.所以它读得很清楚。
If you have marked the parent class members to false you cant expose them, however if its only the parent class that as been marked false (and the member aren't explicitly marked false), you can expose them.如果您已将父类成员标记为 false,则无法公开它们,但是,如果仅将父类标记为 false(并且该成员未明确标记为 false),则可以公开它们。
Otherwise, you will have to re-implement them all again... Sorry, no free lunch here today否则,您将不得不再次重新实现它们...抱歉,今天没有免费午餐
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.