Search code examples
c#.netcomcom-interop

COM Interoperability and Inheritance


I've encountered some issues with a .NET component that I am writing that is intended to be exposed to COM.

The class to be exposed inherits from a base class, both of which have ComVisible properties set to true. There's really nothing special about the base class - its contains 2 properties that I use to help abstract some database code.

I can register the assembly and type lib just fine. However, as soon as I attempt to instantiate the class, I get the following exception thrown from the callee:

Error in IDispatch.Invoke(): 0x80020101

I decided to debug it in CScript.exe and I was getting the same error. So I rewrote the class without the base class, and now I can create instances without issue.

Is there additional work I need to do in order to expose a .NET class that inherits from another .NET class in the same namespace?


Solution

  • You cannot directly use derived interfaces in a scripting language. Scripting engines only supports a coclass that implements a single interface, the default interface. Or to put it another way, scripting languages like vbscript or jscript are not object oriented languages that support multiple inheritance. A workaround is to add a method to the default interface that returns a reference to the second interface. Not commonly done, keeping it simple is boilerplate in scripting.