Compartilhar via


Marcar ComSource interfaces sistema autônomo IDispatch

TypeName

MarkComSourceInterfacesAsIDispatch

CheckId

CA1412

Category (Categoria)

Microsoft.interoperabilidade

Quebrando alterar

Quebrando

Causa

Um tipo é marcado com o System.Runtime.InteropServices.ComSourceInterfacesAttribute atributo e pelo menos um interfaces especificadas não está marcado com o System.Runtime.InteropServices.InterfaceTypeAttribute atributo conjunto para ComInterfaceType.InterfaceIsIDispatch.

Descrição da regra

ComSourceInterfacesAttribute é usado para identificar as interfaces de evento que expõe uma classe para clientes COM. Essas interfaces devem ser expostas sistema autônomo InterfaceIsIDispatch para permitir que sistema autônomo clientes COM do Visual Basic 6 receber notificações de eventos. Por padrão, se uma interface não está marcada com o InterfaceTypeAttribute atributo, ele é exposto sistema autônomo uma interface dupla.

Como corrigir violações

Para corrigir uma violação dessa regra adicionar ou modificar o InterfaceTypeAttribute para que seu valor de atributo conjunto para InterfaceIsIDispatch para todas as interfaces especificadas com o ComSourceInterfacesAttribute atributo.

Quando suprimir avisos

Não suprimir um aviso da regra.

Exemplo

O exemplo a seguir mostra uma classe em que uma das interfaces viola a regra.

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.
   }
}

Regras relacionadas

Não use AutoDual ClassInterfaceType

Consulte também

Tarefas

Como: Elevar eventos tratados por um Coletor COM

Outros recursos

Interoperação com Código Não-Gerenciado