We recommend using Visual Studio 2017

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


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CA1413: Avoid non-public fields in COM visible value types.

|Breaking Change|Breaking|

A value type that is specifically marked as visible to Component Object Model (COM) declares a nonpublic instance field.

Nonpublic instance fields of COM-visible value types are visible to COM clients. Review the content of the field for information that should not be exposed, or that will have an unintended design or security effect.

By default, all public value types are visible to COM. However, to reduce false positives, this rule requires the COM visibility of the type to be explicitly stated. The containing assembly must be marked with the System.Runtime.InteropServices.ComVisibleAttribute set to false and the type must be marked with the ComVisibleAttribute set to true.

To fix a violation of this rule and keep the field hidden, change the value type to a reference type or remove the ComVisibleAttribute attribute from the type.

It is safe to suppress a warning from this rule if public exposure of the field is acceptable.

The following example shows a type that violates the rule.

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(False)>
Namespace InteroperabilityLibrary

   <ComVisibleAttribute(True)> _ 
   Public Structure SomeStructure

      Friend SomeInteger As Integer

   End Structure

End Namespace

CA1407: Avoid static members in COM visible types

CA1017: Mark assemblies with ComVisibleAttribute

Interoperating with Unmanaged Code
Qualifying .NET Types for Interoperation