CA1408:请不要使用 AutoDual ClassInterfaceType

适用范围:yesVisual Studio noVisual Studio for Mac noVisual Studio Code

“值”
RuleId CA1408
Category Microsoft.Interoperability
重大更改 重大

原因

组件对象模型 (COM) 可见类型通过设置为 AutoDualClassInterfaceTypeClassInterfaceAttribute 属性进行标记。

规则说明

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

除非以其他方式标记,否则所有公共非泛型类型均对 COM 可见:所有非公共和泛型类型均对 COM 不可见。

如何解决冲突

若要解决此规则冲突,将 ClassInterfaceAttribute 属性的值更改为 ClassInterfaceTypeNone 值,并显式定义接口。

何时禁止显示警告

除非确定该类型及其基本类型的布局在未来版本中不会改变,否则不要抑制此规则中的警告。

示例

下面的示例演示了违反规则的类,并且重新声明了该类以使用显式接口。

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

另请参阅