N/Direct - The .NET Interoperability Resource Center 





Most Valuable Professional

My classes exported to COM don't contain any methods, even if they are marked with ComVisible(true). What's wrong?
Written: 6/25/2002 Updated: 8/20/2003

This behavior depends on something called the class interface. The class interface is a COM interface that can be generated to contain all the public members of a class exposed to COM. The type library exporter looks for the ClassInterface attribute on your .NET classes to determine if and how to create a class interface. The default, if ClassInterface isn't specified, is to use ClassInterfaceType.AutoDispatch, which means no methods are exported to the type library. The class can then only be used only through late binding, there are no vtable offsets or DISPIDs for clients to cache. This makes AutoDispatch an almost idiot-proof choice from a versioning perspective, at the cost of always having to go through IDispatch late binding. This is a good choice if the component is primarily going to be used from scripting clients or classic ASP.

The next option is ClassInterfaceType.AutoDual. This is the quick and dirty way to get early binding support as well (and make the methods show up in VB6 IntelliSense). But it's also easy to break compatibility, by changing the order of methods or adding new overloads. Avoid using AutoDual.

The last and recommended option is ClassInterfaceType.None. If you use it, the class will not have any class interface. Instead, you have to explicitly define an interface that you implement and then expose to COM. If you later need to add functionality, you add a new interface and implement that as well. This is more like how things were done in COM, and it gives you more control over what's exported and what isn't.

Note that if you're using VB.NET and the ComClass attribute, it uses ClassInterfaceType.None behind the scenes and defines a nested explicit interface for you. However, the ComClass attribute also has versioning issues, so it's not recommended to use this as a shortcut.

The topic Introducing the Class Interface in the documentation explains all this in greater detail.

See also
Introducing the Class Interface
PRB: IntelliSense Features Do Not Appear in Visual Basic 6.0 for .NET COM Interop Components (813809)