C# exposing class to COM - Generic Collections

前端 未结 2 559
旧时难觅i
旧时难觅i 2020-12-18 10:58

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:

相关标签:
2条回答
  • 2020-12-18 11:54

    Check out this post. Either use a straight forward array or use an ArrayList. Seems like a step backwards, but generic collections don't play nice with COM.

    0 讨论(0)
  • 2020-12-18 12:02

    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 { ... }
    }
    
    0 讨论(0)
提交回复
热议问题