Is it possible to implement a COM interface with a .NET generics class?(是否可以使用 .NET 泛型类实现 COM 接口?)
问题描述
我有以下界面,我试图让 COM 可见.当我尝试生成类型库时,它不喜欢我的实现类派生自泛型类的事实.
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 实现类?(我知道我可以编写一个非泛型包装器并将其导出到 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.)
[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).您需要创建非泛型类实现或仅使用接口.
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 接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以使用 .NET 泛型类实现 COM 接口?
				
        
 
            
        - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 
						
						
						
						
						