Share via


CA1412:将 ComSource 接口标记为 IDispatch

类型名

MarkComSourceInterfacesAsIDispatch

CheckId

CA1412

类别

Microsoft.Interoperability

是否重大更改

原因

有一个类型用 ComSourceInterfacesAttribute 特性进行了标记,并且至少有一个指定的接口未用设置为 InterfaceIsDispatch 值的 InterfaceTypeAttribute 特性进行标记。

规则说明

ComSourceInterfacesAttribute 用于标识类向组件对象模型 (COM) 客户端公开的事件接口。 这些接口必须公开为 InterfaceIsIDispatch,以便允许 Visual Basic 6 COM 客户端接收事件通知。 默认情况下,如果接口未用 InterfaceTypeAttribute 特性进行标记,则它将公开为双重接口。

如何解决冲突

若要修复与该规则的冲突,请针对指定了 ComSourceInterfacesAttribute 特性的所有接口,添加或修改 InterfaceTypeAttribute 特性,以便将它的值设置为 InterfaceIsIDispatch。

何时禁止显示警告

不要禁止显示此规则发出的警告。

示例

下面的示例演示一个类,该类的某个接口与此规则冲突。

Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices

<Assembly: ComVisibleAttribute(True)>
Namespace InteroperabilityLibrary

   ' This violates the rule for type EventSource.
   <InterfaceType(ComInterfaceType.InterfaceIsDual)> _ 
   Public Interface IEventsInterface
      Sub EventOne
      Sub EventTwo
   End Interface

   ' This satisfies the rule.
   <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _ 
   Public Interface IMoreEventsInterface
      Sub EventThree
      Sub EventFour
   End Interface

   <ComSourceInterfaces( _
      "InteroperabilityLibrary.IEventsInterface" & _ 
      ControlChars.NullChar & _ 
      "InteroperabilityLibrary.IMoreEventsInterface")> _
   Public Class EventSource
      ' Event and method declarations.
   End Class

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

[assembly: ComVisible(true)]
namespace InteroperabilityLibrary
{
   // This violates the rule for type EventSource.
   [InterfaceType(ComInterfaceType.InterfaceIsDual)]
   public interface IEventsInterface
   {
      void EventOne();
      void EventTwo();
   }

   // This satisfies the rule.
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface IMoreEventsInterface
   {
      void EventThree();
      void EventFour();
   }

   [ComSourceInterfaces(
      "InteroperabilityLibrary.IEventsInterface\0" + 
      "InteroperabilityLibrary.IMoreEventsInterface")]
   public class EventSource
   {
      // Event and method declarations.
   }
}

相关规则

CA1408:请不要使用 AutoDual ClassInterfaceType

请参见

任务

如何:引发 COM 接收器所处理的事件

其他资源

与非托管代码交互操作