是否可以使用.NET泛型类实现COM接口?

时间:2022-02-24 11:34:09

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.

我有以下界面,我正在尝试使COM可见。当我尝试生成类型库时,它不喜欢我的实现类派生自泛型类的事实。

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.)

是否可以将泛型类用作COM实现类? (我知道我可以编写一个非通用的包装器并将其导出到COM,但是这会添加另一个我宁愿不用的层。)

[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.

1 个解决方案

#1


6  

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.

无法导出从泛型类型派生的泛型类型和类型。在MyClass类型上设置ComVisible(false)。您需要创建非泛型类实现或仅使用该接口。

#1


6  

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.

无法导出从泛型类型派生的泛型类型和类型。在MyClass类型上设置ComVisible(false)。您需要创建非泛型类实现或仅使用该接口。