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

查看:184
本文介绍了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互操作的任何类型的。

(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特性(真)] 的类和接口定义,它也没有帮助。

I also tried adding [ComVisible(true)] to the class and interface definitions, and it also did not help.

推荐答案

标记有ComVisible特性的类一般都需要有一个公共的默认构造函数。其成员通常应该也只引用标记有ComVisible特性的类型。

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

您不需要在类中指定标记有ComVisible特性(真),如果你已经在组件级别指定的。

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特性(假)在汇编级。因此,只有那些明确标有标记有ComVisible特性(真)班暴露在COM。

  • 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和说明属性不是必需的,但给你更多的控制COM生成有用的。

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

    如果上面没有帮助,尝试张贴一些示例code,我敢肯定会有人能够帮助。

    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天全站免登陆