2008-09-25 65 views
7

我有下面的接口,我试图让COM可见。当我尝试生成类型库时,它不喜欢我的实现类从泛型类派生的事实。是否可以使用.NET泛型类实现COM接口?

是否可以使用泛型类作为COM实现类? (我知道我可以写一个非一般的包装和导出到COM,但是这增加了另一层,我宁可没有。)

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

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

错误消息:

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.

回答

5

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