是否有可能实现与.NET泛型类的COM接口? [英] Is it possible to implement a COM interface with a .NET generics class?

查看:485
本文介绍了是否有可能实现与.NET泛型类的COM接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的接口,我试图使COM可见。当我尝试生成类型库不喜欢我的实现类从仿制类派生的事实。

是否有可能使用通用类作为一个COM实现类? (我知道我可以写一个非通用包装和出口,为的COM,但是这增加了另一层,我宁愿做没有。)

  [标记有​​ComVisible特性(真)
公共接口IMyClass
{
   ...
}

[标记有ComVisible特性(真),ComDefaultInterface(typeof运算(IMyClass))]
[ClassInterface(ClassInterfaceType.None)
公共类MyClass的:BaseClass的< IMyClass>中IMyClass
{
   ...
}
 

错误消息:

的警告:类型库导出遇到类型派生
从一个普通的类,并没有被标记为
[ClassInterface(ClassInterfaceType.None)]。类接口不能
暴露于这种类型。考虑标记与型式
[ClassInterface(ClassInterfaceType.None)
和揭露显式接口作为默认界面
使用COM的ComDefaultInterface属性。

解决方案

泛型类型和种类,从泛型类型派生不能出口。设置标记有ComVisible特性(假) MyClass的键入。你需要要么只能创建一个非泛型类实现或使用该接口。

I have the following interface which I'm trying to make COM-visible. When I try to generate the type-library it doesn't like the fact that my implementation class derives from a generic-class.

Is it possible to use a generic class as a COM implementation class? (I know I could write a non-generic wrapper and export that to COM, but this adds another layer that I'd rather do without.)

[ComVisible(true)]
public interface IMyClass
{
   ...
}

[ComVisible(true), ComDefaultInterface(typeof(IMyClass))]
[ClassInterface(ClassInterfaceType.None)]
public class MyClass : BaseClass<IMyClass>, IMyClass
{
   ...
}

Error message:

Warning: Type library exporter encountered a type that derives 
from a generic class and is not marked as 
[ClassInterface(ClassInterfaceType.None)]. Class interfaces cannot
be exposed for such types. Consider marking the type with 
[ClassInterface(ClassInterfaceType.None)] 
and exposing an explicit interface as the default interface to 
COM using the ComDefaultInterface attribute.

解决方案

Generic types and types that derive from a generic type cannot be exported. Set ComVisible(false) on your MyClass type. You'll need to either create a non-generic class implementation or use the interface only.

这篇关于是否有可能实现与.NET泛型类的COM接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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