Interop issues

V

Varun Bansal

Hi,

I've written a C# dll and I'm trying to return a "List of structures" to my
C++ client application. I'm getting this warning while compiling my C# dll.

Warning 1 Type library exporter warning processing 'xyz'. Warning: Type
library exporter encountered a generic type instance in a signature. Generic
code may not be exported to COM.

Can someone guide me what needs to be done to resolve it?

Thanks,
Varun
 
G

Guest

COM does not support generic interfaces and therefore you can not directly
expose a generic type. In my opinion you normally shouldn't expose a generic
type directly anyway. I always create a class that derives from the generic
collection to isolate my clients. If you take this route then you can also
implement a custom COM-visible interface that exposes the collection to COM
without impacting the .NET clients. If you explicitly implement the
interface then the .NET clients won't even see the COM-visible interface.
Finally note that ICollection itself is COM-visible so COM clients can use
that as well.

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid(...)]
public class MyComCollection : Collection<myobject>, IMyComCollection
{
int IMyComCollection.Count
{
get { return this.Count; }
}

void IMyComCollection.Add ( myobject obj )
{
this.Add(obj);
}
,,,
}

[ComVisible(true)]
[Guid(...)]
public interface IMyComCollection
{
int Count { get; }
void Add ( myobject obj );
...
}

Note that I highly recommend that all COM visible objects get an explicit
GUID otherwise every time you recompile a new GUID is generated resulting in
registry bloat and causing interop problems. I also recommend that you set
your assembly to not be COM visible by default and that you explicitly mark
each object as visible as needed to avoid unnecessarily publishing objects as
COM visible that you didn't mean to. Finally make sure that you use the
ClassInterfaceAttribute on your COM classes to prevent .NET from
auto-generating an interface for you.

COM interfaces do not support inheritance so avoid using inheritance in
interface definitions for COM. Also note that the first COM visible
interface on a class is considered its default interface and that the
ordering in the code does not necessarily indicate the visibility for this
purpose. .NET 2.0 added a new COM attribute to explicitly identify the
default COM interface for this reason so use it if possible.
 

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