Share via


CA1412: Mark ComSource Interfaces as IDispatch

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
RuleId CA1412
Category Microsoft.Interoperability
Breaking change Breaking

Cause

A type is marked with the ComSourceInterfacesAttribute attribute and at least one specified interface is not marked with the InterfaceTypeAttribute attribute set to the InterfaceIsDispatch value.

Rule description

ComSourceInterfacesAttribute is used to identify the event interfaces that a class exposes to Component Object Model (COM) clients. These interfaces must be exposed as InterfaceIsIDispatch to enable Visual Basic 6 COM clients to receive event notifications. By default, if an interface is not marked with the InterfaceTypeAttribute attribute, it is exposed as a dual interface.

How to fix violations

To fix a violation of this rule, add or modify the InterfaceTypeAttribute attribute so that its value is set to InterfaceIsIDispatch for all interfaces that are specified with the ComSourceInterfacesAttribute attribute.

When to suppress warnings

Do not suppress a warning from this rule.

Example

The following example shows a class where one of the interfaces violates the rule.

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

CA1408: Do not use AutoDual ClassInterfaceType

See also