
Severe VB.NET COM/.NET Interop Limitation
VB.Net beta 2 adds support for a new attribute: ComClass. It can be applied
to a class and will expose the members defined on that class to COM. It is
similar to autodual, but you get a bit more control, and it exposes events.
However, it will not expose the base class members to com, which I think is
what you want. It does not do that because of vesrioning problems. Say MS
adds a new method to system.object in the next version of the runtime. As
soon as it did that, all your com classes would be broken as the vtable
would be changed. I would suggest that what you do is to derive your class,
add the ComClass attribute, and then override all the base class members
that you *need* to expose on your object. In the override definition you can
just call down to the base class member. True it will be a bit ugly, but
then you need to be able to reliably expose yourself to com, that's why the
runtime needs the interfaces to be declared explicitly.
Sam
Quote:
> Thanks for the reply, but you answered all the wrong questions. The
> question is why is VB.NET limited when exposing a class to COM that uses
> inheritance? C# doesn't have this limitation, only VB.NET. If I wanted
to
> expose an object that has four or five ancestors, there is no reasonable
way
> to do this other than by using the
> ClassInterface(ClassInterfaceType.AutoDual on the derived class. Why can
it
> be done "automagically", with extreme limitations vs. being able to be
done
> in a more explicit manner?
> Microsoft really needs to fix this or a serious constraint on VB's ability
> to interoperate with COM exists.
> > inline
> > > Well, it appears that VB.NET users are at an extreme disadvantage when
> > > moving to .NET ***IF*** they expect to make use of both COM Interop
AND
> > > class inheritance.
> > > The docs say that when exposing .NET classes to COM, the best
attribute
> to
> > > use is <ClassInterface(ClassInterfaceType.None)>. What this means is
> > that
> > > your class has to explicitly define a class interface. Unfortunately,
> VB
> > > doesn't support implicit interface implementation, which makes
creating
> a
> > This is a VB.NET design decision to make the interface implementation
> > explicit. It has some advantages (it's absolutely clear which interface
> > contract a method/property satisfies) but it could seem strange to
someone
> > who is used to C++ inheritance.
> > > single exposed interface to COM a tad tedious to do. as well as
> > eliminating
> > > 90% of the reason for using inheritance in the first place. In C#
this
> > The main reason for interface implementation is to provide a contract.
The
> > implementer of the interface satisfies a set of functionality required
by
> a
> > consumer. Inheritance is used to provide polymorphism or to reuse an
> > implementation. In this case since we are talking about interfaces, the
> > former usage applies. The runtime provides full support for interface
> > inheritance including multiple interface inheritance whereas IDL only
> > provides support for single interface inheritance. It would be difficult
> for
> > interop to accurately map multiple interface inheritance.
> > > isn't a problem due to it's ability to implicitly implement an
> interface.
> > > I did come up with a way to do what I want, but I consider it hackish
> and
> > > {*filter*}ugly and I cannot see why the interop team or VB team cannot
come
> up
> > > with a better solution.
> > To give COM consumers of your components the best COM experience you
> should
> > define your interfaces in IDL, compile them with MIDL, import them using
> > tlbimp, and implement these imported interfaces in your managed
component.
> > > As you can see, I have to basically shadow and reimplement the base
> > class's
> > > methods and properties. At this point, why bother using inheritance
at
> > all
> > > as it buys the debeloper very little but causes a lot of grief.
> > I hope this gives you some understanding why these design decisions were
> > maid. As you can see there are several ways to produce a functionality
> that
> > you required.