CA1412: Mark ComSource Interfaces as IDispatch

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

TypeNameMarkComSourceInterfacesAsIDispatch
CheckIdCA1412
CategoryMicrosoft.Interoperability
Breaking ChangeBreaking

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.

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.

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.

Do not suppress a warning from this rule.

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

CA1408: Do not use AutoDual ClassInterfaceType

How to: Raise Events Handled by a COM Sink
Interoperating with Unmanaged Code

Show: