Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#向COM泛型集合公开类_C#_.net_Generics_Com_Generic List - Fatal编程技术网

C#向COM泛型集合公开类

C#向COM泛型集合公开类,c#,.net,generics,com,generic-list,C#,.net,Generics,Com,Generic List,我们有一个用C#.NET2.0编写的小框架,我们希望将其公开给COM 问题是,我们有一些泛型类,它们将公开如下: interface IOurClass { ReadonlyCollection<IOurListObject> OurCollection { get; } } interface IOurListObject { //Some properties that don't matter } 接口IOurClass { 只读集合我们的集合 {

我们有一个用C#.NET2.0编写的小框架,我们希望将其公开给COM

问题是,我们有一些泛型类,它们将公开如下:

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

interface IOurListObject
{
  //Some properties that don't matter
}
接口IOurClass
{
只读集合我们的集合
{
得到;
}
}
接口IOurListObject
{
//一些无关紧要的属性
}
向COM公开泛型集合的最佳方式(或推荐方式)是什么?我们不需要支持泛型,只需要以某种方式公开IOurListObject的集合


我们还希望避免为我们使用的每个集合编写新类,但这可能是不可能的。使用正向数组或ArrayList。这似乎是一种倒退,但通用集合不能很好地使用COM。

不可能向COM公开通用集合(或任何其他“通用”的东西)

因此,我建议您在COM可视接口中创建一个非泛型属性(或方法)。 此方法可以返回“IOurListObject”项的数组。 在您的类中,您可以显式地实现此方法,例如,当您直接在COM外部引用对象时,它不会显示在intellisense中

我希望我能说清楚一点

例如:

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

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

    public ReadOnlyCollection<IOurListObject> OurCollection { ... }
}
[ComVisible(true)]
公共接口类
{
IOurListObject[]我们的集合{get;}
}
公共类:IOurClass
{
IOurListObject[]IOurClass.OurCollection{get{return OurCollection.ToArray();}
公共只读集合我们的集合{…}
}
重复: