Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2006

Tip: Looking for answers? Try searching our database.

does polymorphism work in COM?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gk - 06 Sep 2006 10:06 GMT
-- 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.