共用方式為


CA1412:將 ComSource 介面標記為 IDispatch

型別名稱

MarkComSourceInterfacesAsIDispatch

CheckId

CA1412

分類

Microsoft.Interoperability

中斷變更

中斷

原因

型別已標示為 ComSourceInterfacesAttribute 屬性 (Attribute),且至少有一個指定的介面未標示為已設定成 InterfaceIsDispatch 值的 InterfaceTypeAttribute 屬性。

規則描述

ComSourceInterfacesAttribute 會用來識別類別公開給元件物件模型 (COM) 用戶端的事件介面。這些介面會公開為 InterfaceIsIDispatch,以允許 Visual Basic 6 COM 用戶端接收事件告知。依照預設,如果介面未標記為 InterfaceTypeAttribute 屬性,則該介面會公開為雙重介面 (Dual Interface)。

如何修正違規

若要修正此規則的違規情形,可以加入或修改 InterfaceTypeAttribute 屬性,讓所有已指定 ComSourceInterfacesAttribute 屬性之介面的值可以設定為 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 接收所處理的事件

其他資源

與 Unmanaged 程式碼互通