CA1410:应对 COM 注册方法进行匹配

类型名

ComRegistrationMethodsShouldBeMatched

CheckId

CA1410

类别

Microsoft.Interoperability

是否重大更改

非重大更改

原因

某个类型声明了标记有 ComRegisterFunctionAttribute 特性的方法,但未声明标记有 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) {}
   }
}

相关规则

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

请参见

参考

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

RegistrationServices

概念

向 COM 注册程序集