Compartir a través de


CA1402: Avoid overloads in COM visible interfaces

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 CA1402
Category Microsoft.Interoperability
Breaking change Breaking

Cause

A Component Object Model (COM) visible interface declares overloaded methods.

Rule description

When overloaded methods are exposed to COM clients, only the first method overload retains its name. Subsequent overloads are uniquely renamed by appending to the name an underscore character '_' and an integer that corresponds to the order of declaration of the overload. For example, consider the following methods:

void SomeMethod(int valueOne);
void SomeMethod(int valueOne, int valueTwo, int valueThree);
void SomeMethod(int valueOne, int valueTwo);

These methods are exposed to COM clients as the following.

void SomeMethod(int valueOne);
void SomeMethod_2(int valueOne, int valueTwo, int valueThree);
void SomeMethod_3(int valueOne, int valueTwo);

Visual Basic 6 COM clients cannot implement interface methods by using an underscore in the name.

How to fix violations

To fix a violation of this rule, rename the overloaded methods so that the names are unique. Alternatively, make the interface invisible to COM by changing the accessibility to internal (Friend in Visual Basic) or by applying the System.Runtime.InteropServices.ComVisibleAttribute attribute set to false.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows an interface that violates the rule and an interface that satisfies the rule.

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(False)>
Namespace InteroperabilityLibrary

   ' This interface violates the rule.
   <ComVisibleAttribute(True)> _ 
   Public Interface IOverloadedInterface

      Sub SomeSub(valueOne As Integer)
      Sub SomeSub(valueOne As Integer, valueTwo As Integer)

   End Interface

   ' This interface satisfies the rule.
   <ComVisibleAttribute(True)> _ 
   Public Interface INotOverloadedInterface

      Sub SomeSub(valueOne As Integer)
      Sub AnotherSub(valueOne As Integer, valueTwo As Integer)

   End Interface

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

[assembly: ComVisible(false)]
namespace InteroperabilityLibrary
{
   // This interface violates the rule.
   [ComVisible(true)]
   public interface IOverloadedInterface
   {
      void SomeMethod(int valueOne);
      void SomeMethod(int valueOne, int valueTwo);
   }

   // This interface satisfies the rule.
   [ComVisible(true)]
   public interface INotOverloadedInterface
   {
      void SomeMethod(int valueOne);
      void AnotherMethod(int valueOne, int valueTwo);
   }
}

CA1413: Avoid non-public fields in COM visible value types

CA1407: Avoid static members in COM visible types

CA1017: Mark assemblies with ComVisibleAttribute

See also