简体   繁体   English

.NET:使对象及其所有从属对象COM可见

[英].NET: Make an object and all of its subordinate objects COM visible

I've got a proprietary assembly with a class, which needs to be COM visible, so I can use it with other (non .NET) applications. 我有一个带有类的专有程序集,该程序集必须是COM可见的,因此我可以将其与其他(非.NET)应用程序一起使用。

Imagine a class like this: 想象一个这样的类:

public class CProprietary
{
    public CProprietary2 oSubItem;
}

public class CProprietary2
{
}

These classes are proprietary and not COM visible. 这些类是专有的,对COM不可见。 I don't have the source code of them. 我没有它们的源代码。 As current solution I am creating my own assembly which provides a inherited, COM visible class. 作为当前解决方案,我正在创建自己的程序集,该程序集提供了继承的COM可见类。

[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class CComVisibleProprietary : CProprietary
{
}

Now I am able to use the base class (CProprietary) via COM, but the object of the subordinate class (CProprietary2) is still returned non COM visible when accessing CComVisibleProprietary.oSubItem. 现在,我可以通过COM使用基类(CProprietary),但是在访问CComVisibleProprietary.oSubItem时,仍然返回非COM可见的下级类(CProprietary2)的对象。

So my problem is that I need a way to automatically wrap a class and all of its child classes. 所以我的问题是我需要一种自动包装一个类及其所有子类的方法。 Or is there any easy way to inherit the ComVisible attribute to the child classes? 还是有任何简单的方法可以将ComVisible属性继承到子类? Some properties of these proprietary classes also return objects of .NET like System.Windows.Forms.ListBox.ObjectCollection, so they don't belong to the same namespace. 这些专有类的某些属性还会返回.NET的对象,例如System.Windows.Forms.ListBox.ObjectCollection,因此它们不属于同一名称空间。

The correct way from the 'automation view' would be to create own objects for each embedded object (CProprietary2). 从“自动化视图”正确的方法是为每个嵌入式对象(CProprietary2)创建自己的对象。 Each embedded object may be accessed by using a property in the main object (CProprietary) and returning the interface to the embedded object. 可以通过使用主对象(CProprietary)中的属性并将接口返回到嵌入式对象来访问每个嵌入式对象。

But there is no automatic way to achive this. 但是没有自动的方法来实现这一目标。 You must provide corresponding interfaces to all objects. 您必须为所有对象提供相应的接口。

The syntax in VBScript, when prop is of type CPropriatary, you might access the sub item in this way. 如果prop的类型为CPropriatary,则VBScript中的语法可能会以这种方式访问​​子项。

prop.oSubItem.DoSomething

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM