简体   繁体   English

公开 .NET 类函数而不包装它们

[英]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.我有一个COMVisibleMyAPI ,我的客户将使用它作为所有 API 访问的根对象。

I have my APIs in another assembly OtherAPI that has Document and Window API classes (not COM visible).我的 API 位于另一个程序集OtherAPI 中,该程序集具有DocumentWindow API 类(COM 不可见)。

I want to expose Document and Window classes via MyAPI class to a late binding client such as JavaScript.我想通过MyAPI类向后期绑定客户端(如 JavaScript)公开DocumentWindow类。

[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.

相关问题 如何从抽象类访问函数而不使它们静态? - How to access functions from abstract class without making them static? ASP.NET将控件公开给其他类 - ASP.NET expose controls to other class 如果在与 .net 6 相同的浏览器中使用,如何将页面公开为 iframe 并隔离它们 - how to expose page as iframe and isolate them if used in same browser with .net 6 带有任意元组作为关键字的字典(类中没有包装) - Dictionary with Arbitrary Tuple for Key (Without Wrapping in a Class) 如何在c#中没有引用的情况下将类暴露给外部项目 - How to expose a class to an external Project without Reference in c# 公开引用类型(类)而无需额外引用 - expose a referenced type (class) without need for additional reference 将 .net 4 class 库中的 C# 类公开给 Silverlight 应用程序 - Expose C# classes in .net 4 class library to Silverlight App 是否可以在.NET中向外部公开类的成员对象的事件? - Is it possible to expose events of a member object of a class to the outside in .NET? .NET Observable 包装在一个类中以在接口上公开另一个 Observable - .NET Observable wrapped in a class to expose another Observable on the interface C#将Xml属性反序列化为值,而不用类包装它 - C# deserialize Xml attribute to value without wrapping it with class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM