ComVisible and generics

A

Adam Clauss

I am working on modifying an existing library to expose portions through
COM.
Is it possible to return a generic object (say, List<string>) via this?

So far I have not had much luck. When components call my method which
returns a List<string> (ex: javascript), the call succeeds, and SOMETHING
comes back, but I cannot seem to locate any properties/values on the
returned object?

I have tried to do some searching on this, but "COM" and "Generic(s)" are
rather hard terms to search on and get meaningful results :)

Any thoughts on this? Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Adam,

Well, generics and COM don't mix. Remember, COM is an interface-based
framework, which depends on the contract (in this case, an interface) being
defined, and immutable, through which you access implementations.

With Generics, the contract is never really "known" since you have the
issue of type parameters. Because type parameters can be applied on the
method level and on the class level, you can never be sure what the class
interface is going to be.

This is why you don't see Generic types marked with the ComVisible
attribute. When you pass that List<string> type to COM, you see something,
definitely, and that is because Object is marked with ComVisible, and you
see everything on that level (which is nothing).

What you could also do is create a type like this:

[ComVisible]
class StringList : List<string> {}

And then use types of StringList throughout your COM code.

Hope this helps.
 
A

Adam Clauss

Nicholas Paldino said:
Adam,

Well, generics and COM don't mix. Remember, COM is an interface-based
framework, which depends on the contract (in this case, an interface)
being defined, and immutable, through which you access implementations.

With Generics, the contract is never really "known" since you have the
issue of type parameters. Because type parameters can be applied on the
method level and on the class level, you can never be sure what the class
interface is going to be.

This is why you don't see Generic types marked with the ComVisible
attribute. When you pass that List<string> type to COM, you see
something, definitely, and that is because Object is marked with
ComVisible, and you see everything on that level (which is nothing).

Unfortunately that is what I was afraid you were going to say hehe. I was
hoping there might be some "magic' to make things work.
What you could also do is create a type like this:

[ComVisible]
class StringList : List<string> {}

And then use types of StringList throughout your COM code.

I had earlier given something like this a try and ran into problems, but I
will attempt it again. Question - would the existance of SOME members
(methods, etc) that have generic parameters/return values (like some of
List's do) cause the entire object to be unusable?
 
N

Nicholas Paldino [.NET/C# MVP]

Adam,

I don't know that it will cause the entire list to be unusable, but you
definitely can't use those members.

Mind you, that the proper way to perform COM interop is to define an
interface that doesn't change (which obviously can't be Generic) and then
implement that interface, and access that, not through the type.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adam Clauss said:
Nicholas Paldino said:
Adam,

Well, generics and COM don't mix. Remember, COM is an interface-based
framework, which depends on the contract (in this case, an interface)
being defined, and immutable, through which you access implementations.

With Generics, the contract is never really "known" since you have the
issue of type parameters. Because type parameters can be applied on the
method level and on the class level, you can never be sure what the class
interface is going to be.

This is why you don't see Generic types marked with the ComVisible
attribute. When you pass that List<string> type to COM, you see
something, definitely, and that is because Object is marked with
ComVisible, and you see everything on that level (which is nothing).

Unfortunately that is what I was afraid you were going to say hehe. I was
hoping there might be some "magic' to make things work.
What you could also do is create a type like this:

[ComVisible]
class StringList : List<string> {}

And then use types of StringList throughout your COM code.

I had earlier given something like this a try and ran into problems, but I
will attempt it again. Question - would the existance of SOME members
(methods, etc) that have generic parameters/return values (like some of
List's do) cause the entire object to be unusable?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top