Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

We have a small framework written in C# .Net 2.0 that we want to expose to COM.

Problem is, we have some generic classes that would be exposed as the following:

interface IOurClass
{
  ReadonlyCollection<IOurListObject> OurCollection
  {
    get;
  }
}

interface IOurListObject
{
  //Some properties that don't matter
}

What is the best (or recommended way) to expose generic collections to COM? We do not have to support generics, we just need to somehow expose a collection of IOurListObject.

We also would like to avoid having to write a new class for every collection we use, but it may not be possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
706 views
Welcome To Ask or Share your Answers For Others

1 Answer

It is not possible to expose generic collections (or any other thing that is 'generic') to COM.

So, I suggest that you create a non-generic property (or method) in your COM-visible interface. This method could return an array of 'IOurListObject' items. In your class, you could implement this method explicitly, so that it does not show up in your intellisense when you refer to the object directly outside COM, for instance.

I hope I make myself a bit clear.

Example:

[ComVisible(true)]
public interface IOurClass
{
    IOurListObject[] OurCollection { get; }
}

public class OurClass : IOurClass
{
    IOurListObject[] IOurClass.OurCollection { get { return OurCollection.ToArray();} }

    public ReadOnlyCollection<IOurListObject> OurCollection { ... }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...