在服务组件上使用通用列表 [英] Using Generic lists on serviced component

查看:109
本文介绍了在服务组件上使用通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用一个通用列表作为 ServicedComponent 类的属性...

I'm trying to use a generic List as a property on a ServicedComponent class...

public class MyCOM : ServicedComponent {
    public enum MyEnumType {
        Value1, Value2, Value3
    }
    public List<MyEnumType> MyList { private set; get; }
    public MyCOM()
    {
        MyList = new List<MyEnumType>();
    }
}

代码编译时没有错误,但是当我尝试使用来自不同类的com对象的 MyList 属性没有值添加到列表。 Google告诉我我不能使用Generics on Components,但是我还没有找到一个很好的解释,为什么这是一个很好的解决方案。

The code compiles without errors but when I try to use the MyList property on the com object from a different class no values get added to the list. Google "told me" that I can't use Generics on Components but I'm yet to find a good explanation why that is and a good solution to the problem.

有人可以帮我吗?

推荐答案

来自MSDN:


使用通用类型互操作

COM模型不支持通用类型的
概念。
因此,泛型类型不能是
直接用于COM互操作。

The COM model does not support the concept of generic types. Consequently, generic types cannot be used directly for COM interop.

为什么不支持泛型非常简单,泛型是在运行时构造的类型,并且因为没有静态接口声明到COM可以引用的构造类型。在你的情况List< MyEnumType>不作为类型存在直到CLR构造它,所以COM不能引用它和标识符(GUID)。

The answer why generics are not supported is very simple, generics are types that are constructed at runtime, and because of this there's no static interface declaraion to the constructed type that COM can refer to. In your case List< MyEnumType > doesn't exist as a type Until the CLR constructs it, so COM can't refer to it with and identifier (GUID).

这是解决方法来自,如果你的泛型类型实现一个非通用接口,那么它们可以用于使用非通用接口的com互操作。

This is where the workaround comes from, if your generic types implement a non generic interface, then they can be used for com interop using the non generic interface.

这篇关于在服务组件上使用通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆