Share via


CA1410: COM registration methods should be matched

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. 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
RuleId CA1410
Category Microsoft.Interoperability
Breaking change Non-breaking

Cause

A type declares a method that is marked with the System.Runtime.InteropServices.ComRegisterFunctionAttribute attribute but does not declare a method that is marked with the System.Runtime.InteropServices.ComUnregisterFunctionAttribute attribute, or vice versa.

Rule description

For Component Object Model (COM) clients to create a .NET type, the type must first be registered. If it is available, a method that is marked with the ComRegisterFunctionAttribute attribute is called during the registration process to run user-specified code. A corresponding method that is marked with the ComUnregisterFunctionAttribute attribute is called during the unregistration process to reverse the operations of the registration method.

How to fix violations

To fix a violation of this rule, add the corresponding registration or unregistration method.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows a type that violates the rule. The commented code shows the fix for the violation.

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

CA1411: COM registration methods should not be visible

See also