Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 3.06 KB

ca1403.md

File metadata and controls

68 lines (47 loc) · 3.06 KB
title description ms.date ms.topic f1_keywords helpviewer_keywords author ms.author manager ms.subservice dev_langs monikerRange
CA1403: Auto layout types should not be COM visible
A COM-visible value type is marked with the System.Runtime.InteropServices.StructLayoutAttribute attribute set to LayoutKind.Auto.
11/04/2016
reference
AutoLayoutTypesShouldNotBeComVisible
CA1403
CA1403
AutoLayoutTypesShouldNotBeComVisible
mikejo5000
mikejo
mijacobs
code-analysis
CSharp
VB
vs-2019

CA1403: Auto layout types should not be COM visible

Item Value
RuleId CA1403
Category Microsoft.Interoperability
Breaking change Breaking

Cause

A Component Object Model (COM) visible value type is marked with the xref:System.Runtime.InteropServices.StructLayoutAttribute?displayProperty=fullName attribute set to xref:System.Runtime.InteropServices.LayoutKind.Auto?displayProperty=fullName.

Rule description

xref:System.Runtime.InteropServices.LayoutKind layout types are managed by the common language runtime. The layout of these types can change between versions of .NET, which breaks COM clients that expect a specific layout. If the xref:System.Runtime.InteropServices.StructLayoutAttribute attribute is not specified, the C#, Visual Basic, and C++ compilers specify LayoutKind.Auto for value types.

Unless marked otherwise, all public, non-generic types are visible to COM, and 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 xref:System.Runtime.InteropServices.ComVisibleAttribute?displayProperty=fullName set to false and the type must be marked with the xref:System.Runtime.InteropServices.ComVisibleAttribute set to true.

How to fix violations

To fix a violation of this rule, change the value of the xref:System.Runtime.InteropServices.StructLayoutAttribute attribute to LayoutKind.Explicit or LayoutKind.Sequential, or make the type invisible to COM.

When to suppress warnings

Do not suppress a warning from this rule.

Example

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

:::code language="csharp" source="../snippets/csharp/VS_Snippets_CodeAnalysis/FxCop.Interoperability.AutoLayout/cs/FxCop.Interoperability.AutoLayout.cs" id="Snippet1":::

:::code language="vb" source="../snippets/visualbasic/VS_Snippets_CodeAnalysis/FxCop.Interoperability.AutoLayout/vb/FxCop.Interoperability.AutoLayout.vb" id="Snippet1":::

Related rules

CA1408: Do not use AutoDual ClassInterfaceType

See also