Export (0) Print
Expand All

Auto layout types should not be ComVisible

TypeName

AutoLayoutTypesShouldNotBeComVisible

CheckId

CA1403

Category

Microsoft.Interoperability

Breaking Change

Breaking

Auto layout types are managed by the common language runtime. The layout of these types can change between versions of the .NET Framework, which will break COM clients that expect a specific layout. Note that if the StructLayoutAttribute attribute is not specified, the C#, Visual Basic, and C++ compilers specify the Sequential layout for value types.

Unless marked otherwise, all public non-generic types are visible to COM; all non-public and generic types are invisible 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 change the value of the StructLayoutAttribute attribute to Explicit or Sequential, or make the type invisible to COM.

Do not exclude a warning from this rule.

The following example shows a type that violates the rule and a type that satisfies the rule.

using System;
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
namespace InteroperabilityLibrary
{
   // This violates the rule.
   [StructLayout(LayoutKind.Auto)]
   [ComVisible(true)]
   public struct AutoLayout
   {
      public int ValueOne;
      public int ValueTwo;
   }

   // This satisfies the rule.
   [StructLayout(LayoutKind.Explicit)]
   [ComVisible(true)]
   public struct ExplicitLayout
   {
      [FieldOffset(0)]
      public int ValueOne;

      [FieldOffset(4)]
      public  int ValueTwo;
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft