Share via


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

类型名

ComRegistrationMethodsShouldNotBeVisible

CheckId

CA1411

类别

Microsoft.Interoperability

是否重大更改

原因

标记有 System.Runtime.InteropServices.ComRegisterFunctionAttributeSystem.Runtime.InteropServices.ComUnregisterFunctionAttribute 特性的方法在外部可见。

规则说明

在向组件对象模型 (COM) 注册程序集时,会针对该程序集中的每个 COM 可见类型向注册表中添加项。 标记有 ComRegisterFunctionAttributeComUnregisterFunctionAttribute 特性的方法分别会在注册和注销过程中被调用,以便运行特定于这些类型的注册/注销的用户代码。 此代码不应当在这些进程外部调用。

如何解决冲突

若要修复与该规则的冲突,请将方法的可访问性更改为 private 或 internal(在 Visual Basic 中为 Friend)。

何时禁止显示警告

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

示例

下面的示例演示两个与该规则冲突的方法。

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(True)>
Namespace InteroperabilityLibrary

   Public Class ClassToRegister
   End Class

   Public Class ComRegistration

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

      <ComUnregisterFunctionAttribute> _ 
      Public 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]
      public static void RegisterFunction(Type typeToRegister) {}

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

相关规则

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

请参见

参考

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

System.Runtime.InteropServices.RegistrationServices

概念

向 COM 注册程序集