2008-09-25 88 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)。您需要創建一個非泛型類實現或僅使用該接口。