Share via


CA1408:请不要使用 AutoDual ClassInterfaceType

类型名

DoNotUseAutoDualClassInterfaceType

CheckId

CA1408

类别

Microsoft.Interoperability

是否重大更改

原因

通过将 ClassInterfaceAttribute 属性设置为 ClassInterfaceType 的 AutoDual 值来标记组件对象模型 (COM) 可见的类型。

规则说明

使用双重接口的类型使客户端可以绑定到特定的接口布局。 如果在将来的版本中对该类型或任何基类型的布局进行更改,将中断绑定到该接口的 COM 客户端。 默认情况下,如果未指定 ClassInterfaceAttribute 特性,将使用仅支持调度的接口。

除非另行标记,否则所有公共的非泛型类型都对 COM 可见;所有非公共类型和泛型类型都对 COM 不可见。

如何解决冲突

若要修复与该规则的冲突,请将 ClassInterfaceAttribute 特性的值更改为 ClassInterfaceType 的 None 值并显式定义该接口。

何时禁止显示警告

除非可以肯定该类型及其基类型的布局不会在将来的版本中更改,否则不要禁止显示此规则发出的警告。

示例

下面的示例演示一个与该规则冲突的类以及为使用显式接口而对该类的重新声明。

Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(True)>
Namespace InteroperabilityLibrary

   ' This violates the rule.
   <ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _ 
   Public Class DualInterface
      Public Sub SomeSub
      End Sub
   End Class

   Public Interface IExplicitInterface
      Sub SomeSub
   End Interface

   <ClassInterfaceAttribute(ClassInterfaceType.None)> _ 
   Public Class ExplicitInterface
      Implements IExplicitInterface

      Public Sub SomeSub Implements IExplicitInterface.SomeSub
      End Sub

   End Class

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

[assembly: ComVisible(true)]
namespace InteroperabilityLibrary
{
   // This violates the rule.
   [ClassInterface(ClassInterfaceType.AutoDual)]
   public class DualInterface
   {
      public void SomeMethod() {}
   }

   public interface IExplicitInterface
   {
      void SomeMethod();
   }

   [ClassInterface(ClassInterfaceType.None)]
   public class ExplicitInterface : IExplicitInterface
   {
      public void SomeMethod() {}
   }
}

相关规则

CA1403:自动布局类型不应对 COM 可见

CA1412:将 ComSource 接口标记为 IDispatch

请参见

概念

类接口简介

为互操作限定 .NET 类型

其他资源

与非托管代码交互操作