Share via


CA1405: COM visible type base types should be COM visible

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

Cause

A Component Object Model (COM) visible type derives from a type that is not COM visible.

Rule description

When a COM visible type adds members in a new version, it must abide by strict guidelines to avoid breaking COM clients that bind to the current version. A type that is invisible to COM presumes it does not have to follow these COM versioning rules when it adds new members. However, if a COM visible type derives from the COM invisible type and exposes a class interface of System.Runtime.InteropServices.ClassInterfaceType or ClassInterfaceType (the default), all public members of the base type (unless they are specifically marked as COM invisible, which would be redundant) are exposed to COM. If the base type adds new members in a subsequent version, any COM clients that bind to the class interface of the derived type might break. COM visible types should derive only from COM visible types to reduce the chance of breaking COM clients.

How to fix violations

To fix a violation of this rule, make the base types COM visible or the derived type COM invisible.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows a type that violates the rule.

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(False)>
Namespace InteroperabilityLibrary

   <ComVisibleAttribute(False)> _ 
   Public Class BaseClass

      Sub SomeSub(valueOne As Integer)
      End Sub

   End Class

   ' This class violates the rule.
   <ComVisibleAttribute(True)> _ 
   Public Class DerivedClass
      Inherits BaseClass

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

   End Class

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

[assembly: ComVisible(false)]
namespace InteroperabilityLibrary
{
   [ComVisible(false)]
   public class BaseClass
   {
      public void SomeMethod(int valueOne) {}
   }

   // This class violates the rule.
   [ComVisible(true)]
   public class DerivedClass : BaseClass
   {
      public void AnotherMethod(int valueOne, int valueTwo) {}
   }
}

See also