是否可以使用.NET泛型类实现COM接口?
时间:2020-03-06 14:44:05 来源:igfitidea点击:
我有以下接口,试图使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.
解决方案
泛型类型和从泛型类型派生的类型不能导出。在MyClass类型上设置ComVisible(false)。我们将需要创建非泛型类实现或者仅使用接口。