应对 COM 注册方法进行匹配

更新:2007 年 11 月

TypeName

ComRegistrationMethodsShouldBeMatched

CheckId

CA1410

类别

Microsoft.Interoperability

是否重大更改

原因

某个类型声明了标记有 System.Runtime.InteropServices.ComRegisterFunctionAttribute 属性的方法,但未声明标记有 System.Runtime.InteropServices.ComUnregisterFunctionAttribute 属性的方法,或者反过来。

规则说明

为了让 COM 客户端能够创建 .NET Framework 类型,首先必须注册该类型。在注册过程中将调用标记有 ComRegisterFunctionAttribute 属性的方法(如果有的话),以运行用户指定的代码。注销过程中将调用标记有 ComUnregisterFunctionAttribute 属性的对应方法,以执行与注册方法的操作相反的操作。

如何修复冲突

若要修复与该规则的冲突,请添加对应的注册或注销方法。

何时禁止显示警告

不要禁止显示此规则发出的警告。

示例

下面的示例演示一个与该规则冲突的类型。带注释的代码演示对该冲突的修复。

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(True)>
Namespace InteroperabilityLibrary

   Public Class ClassToRegister
   End Class

   Public Class ComRegistration

      <ComRegisterFunctionAttribute> _ 
      Friend Shared Sub RegisterFunction(typeToRegister As Type)
      End Sub

'      <ComUnregisterFunctionAttribute> _ 
'      Friend Shared Sub UnregisterFunction(typeToRegister As Type)
'      End Sub

   End Class

End Namespace
using System;
using System.Runtime.InteropServices;

[assembly: ComVisible(true)]
namespace InteroperabilityLibrary
{
   public class ClassToRegister
   {
   }

   public class ComRegistration
   {
      [ComRegisterFunction]
      internal static void RegisterFunction(Type typeToRegister) {}

//      [ComUnregisterFunction]
//      internal static void UnregisterFunction(Type typeToRegister) {}
   }
}

相关规则

COM 注册方法不应该是可见的

请参见

概念

向 COM 注册程序集

参考

程序集注册工具 (Regasm.exe)

System.Runtime.InteropServices.RegistrationServices