更新:2007 年 11 月
TypeName |
DoNotUseAutoDualClassInterfaceType |
CheckId |
CA1408 |
类别 |
Microsoft.Interoperability |
是否重大更改 |
是 |
原因
COM 可见类型标记有设置为 ClassInterfaceType.AutoDual 的 System.Runtime.InteropServices.ClassInterfaceAttribute 属性。
规则说明
使用双重接口的类型允许将客户端绑定到特定的接口布局。如果在将来的版本中对该类型或任何基类型的布局进行更改,将中断绑定到该接口的 COM 客户端。默认情况下,如果未指定 ClassInterfaceAttribute 属性,将使用仅支持调度的接口。
除非另行标记,否则所有公共的非泛型类型都对 COM 可见;所有非公共类型和泛型类型都对 COM 不可见。
如何修复冲突
若要修复与该规则的冲突,请将 ClassInterfaceAttribute 属性的值更改为 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() {}
}
}