-- 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,
gk
gk - 06 Sep 2006 14:29 GMT
> -- begin code --
> // strongly typed list of base class
[quoted text clipped - 17 lines]
>
> gk
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 --
gk