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

发布于 2024-07-05 23:23:55 字数 805 浏览 6 评论 0原文

我有以下接口,我试图使 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.

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.

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
{
   ...
} 

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-07-12 23:23:55

无法导出泛型类型和从泛型类型派生的类型。 在您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文