Examples

The following example demonstrates how to apply IDispatchImplAttribute to select the IDispatch implementation supplied by the common language runtime.

using namespace System;
using namespace System::Runtime::InteropServices;
// by default all classes in this assembly will use COM implementaion
// // But this class will use runtime implementaion
[assembly:IDispatchImpl(IDispatchImplType::CompatibleImpl)];
[IDispatchImpl(IDispatchImplType::InternalImpl)]
ref class MyClass{};

using System;
using System.Runtime.InteropServices;
// by default all classes in this assembly will use COM implementaion
[assembly:IDispatchImpl(IDispatchImplType.CompatibleImpl)]
namespace MyNamespace
{
// But this class will use runtime implementaion
[IDispatchImpl(IDispatchImplType.InternalImpl)]
class MyClass
{
//
}
}

Imports System
Imports System.Runtime.InteropServices
' by default all classes in this assembly will use COM implementaion
<Assembly: IDispatchImpl(IDispatchImplType.CompatibleImpl)>
Module MyNamespace
' But this class will use runtime implementaion
<IDispatchImpl(IDispatchImplType.InternalImpl)> _
Public Class c
'
End Class
End Module

Remarks

You can apply this attribute to classes or assemblies.

This attribute should only be set when an explicit implementation is required. When you set the attribute to CompatibleImpl, the IDispatch implementation is supplied by passing the type information for the object to COM's CreateStdDispatch API. When you set the attribute to InternalImpl, the IDispatch implementation is supplied by the common language runtime. Setting the attribute to SystemDefinedImpl allows the runtime to choose the appropriate implementation. When using the attribute on an assembly, the attribute applies to all classes defined within the assembly. When using the attribute on an individual class, the attribute applies only to the interfaces exposed by that class and overrides any assembly-level setting.