COM互操作注册 [英] COM Interop registration

查看:1236
本文介绍了COM互操作注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET程序集,我会暴露给 COM 。程序集有两个公共接口和一个公共类。当我构建程序集时,我得到这个警告:

I have a .NET assembly which I am exposing to COM. The assembly has two public interfaces and one public class. When I build the assembly I get this warning:


(assemblyName.dll)不包含任何可以注册COM Interop的类型。

(assemblyName.dll) does not contain any types that can be registered for COM Interop.

我的程序集信息包括以下行。

My assembly information includes the following line.

[assembly: ComVisible(true)]

大多数人在网上有这个问题,我发现,它固定它的上面的行在他们的程序集信息。这对我没有帮助。

Most people having this problem on the web, that I have found, fixed it with the above line in their assembly information. This has not helped for me.

我也试过添加 [ComVisible(true)]

推荐答案

ComVisible类通常需要有一个公共的默认构造函数。它的成员通常也应该只引用ComVisible类型。

ComVisible classes generally need to have a public default constructor. Its members should typically also reference only ComVisible types.

如果在组件级别指定了ComVisible(true),则不需要在类上指定ComVisible(true)。

You don't need to specify ComVisible(true) on the class if you have specified it at the assembly level.

但是,使用ComVisible类生成程序集的常见方法是:

However, the usual way to generate an assembly with ComVisible classes is:


  • 在程序集级别指定ComVisible(false)。

  • Specify ComVisible(false) at assembly-level. Thus only classes that are explicitly marked with ComVisible(true) are exposed to COM.

定义一个显式的ComVisible接口:

Define an explicit ComVisible interface :

例如

[
ComVisible(true),
GuidAttribute("..."),
Description("...")
]
public interface IMyComVisibleType
{
        // members...
     }




  • 您的ComVisible类应该指定ClassInterfaceType.None,并且应该实现ComVisible接口:

  • 例如

         [
         ComVisible(true),
         GuidAttribute("..."),
         ClassInterface(ClassInterfaceType.None)
         ]
         public sealed class MyComVisibleType : IMyComVisibleType
         {
            // implementation ...
         }
    

    注意,Guid和Description属性不是必需的,有用的是给你更多的COM生成控制。

    Note that the Guid and Description attributes are not required, but useful to give you more control of the COM generation.

    如果上面的没有帮助,请尝试发布一些示例代码,我相信有人能够帮助。

    If the above doesn't help, try posting some sample code and I'm sure someone will be able to help.

    这篇关于COM互操作注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆