Share via


CA1411: COM registration methods should not be visible

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName ComRegistrationMethodsShouldNotBeVisible
CheckId CA1411
Category Microsoft.Interoperability
Breaking Change Breaking

Cause

A method that is marked with the System.Runtime.InteropServices.ComRegisterFunctionAttribute or the System.Runtime.InteropServices.ComUnregisterFunctionAttribute attribute is externally visible.

Rule Description

When an assembly is registered with Component Object Model (COM), entries are added to the registry for each COM-visible type in the assembly. Methods that are marked with the ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute attributes are called during the registration and unregistration processes, respectively, to run user code that is specific to the registration/unregistration of these types. This code should not be called outside these processes.

How to Fix Violations

To fix a violation of this rule, change the accessibility of the method to private or internal (Friend in Visual Basic).

When to Suppress Warnings

Do not suppress a warning from this rule.

Example

The following example shows two methods that violate the rule.

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) {}
   }
}

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

CA1410: COM registration methods should be matched

See Also

System.Runtime.InteropServices.RegistrationServices Registering Assemblies with COM Regasm.exe (Assembly Registration Tool)