Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CA1402: Avoid overloads in COM visible interfaces

TypeName

AvoidOverloadsInComVisibleInterfaces

CheckId

CA1402

Category

Microsoft.Interoperability

Breaking Change

Breaking

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

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.

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.

Do not suppress a warning from this rule.

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

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);
   }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.