Search code examples
c#.netcomactivexcomvisible

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

Imagine a class like this:

public class CProprietary
{
    public CProprietary2 oSubItem;
}

public class CProprietary2
{
}

These classes are proprietary and not COM visible. 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.

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

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


Solution

  • The correct way from the 'automation view' would be to create own objects for each embedded object (CProprietary2). Each embedded object may be accessed by using a property in the main object (CProprietary) and returning the interface to the embedded object.

    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.oSubItem.DoSomething