all groups > dotnet interop > september 2006 >
You're in the

dotnet interop

group:

does polymorphism work in COM?


does polymorphism work in COM? gk
9/6/2006 12:00:00 AM
dotnet interop:
-- begin code --
// strongly typed list of base class
List<Base> MyList = new List<Base>();

//the method i want to expose to a COM scripting client, which returns
MyList elements by index (because COM doesn't support generic lists)
Base Get(int index) {}
-- end code --

The "Base" class has no COM interface, but the class "Derived"
(descendant of "Base") has.

The problem is when I fill the list with instances of "Derived", the
"Get" interface method returns nothing instead of the interface to
"Derived".

So doesn't polymorphism work in COM? How can I solve this?


Greetings,

Re: does polymorphism work in COM? gk
9/6/2006 3:29:24 PM
[quoted text, click to view]

to answer my own post:

2 reasons were causing the failure

1) the base class was not visible to COM [ComVisibleAttribute(false)].
Because it is an abstract class, I thought that this would keep things
clean when viewed from COM. But apparently this makes polymorphism
impossible. After using [ComVisibleAttribute(true)], the "Get" method
was returning an interface. However, I could not use it because it's
methods and properties were NULL.

2) the "Get" method return type is "Base", but the actual return value
is a reference to "Derived". Adding the MarshalAs attribute to the
interface solved this problem.
-- begin code --
[return: MarshalAs(UnmanagedType.IDispatch)]
Base Get(int index);
-- end code --


AddThis Social Bookmark Button