简体   繁体   English

C#将类暴露给COM - 通用集合

[英]C# exposing class to COM - Generic Collections

We have a small framework written in C# .Net 2.0 that we want to expose to COM. 我们有一个用C#.Net 2.0编写的小框架,我们想要向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? 将通用集合暴露给COM的最佳(或推荐方法)是什么? We do not have to support generics, we just need to somehow expose a collection of IOurListObject. 我们不必支持泛型,我们只需要以某种方式公开IOurListObject的集合。

We also would like to avoid having to write a new class for every collection we use, but it may not be possible. 我们也希望避免为我们使用的每个集合编写一个新类,但它可能是不可能的。

It is not possible to expose generic collections (or any other thing that is 'generic') to COM. 不可能将通用集合(或任何其他“通用”的东西)暴露给COM。

So, I suggest that you create a non-generic property (or method) in your COM-visible interface. 所以,我建议你在COM-visible界面中创建一个非泛型属性(或方法)。 This method could return an array of 'IOurListObject' items. 此方法可以返回'IOurListObject'项的数组。 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. 在你的类中,你可以显式地实现这个方法,这样当你直接在COM之外引用对象时它就不会出现在你的intellisense中。

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 { ... }
}

Check out this post . 看看这篇文章 Either use a straight forward array or use an ArrayList. 使用直接数组或使用ArrayList。 Seems like a step backwards, but generic collections don't play nice with COM. 看起来像向后退一步,但通用集合不适合COM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM