IDispatchImpl Class
Summary
Represents the implementation of the IDispatch
interface for COM interop scenarios. This class provides a way to expose .NET objects to COM clients in a late-bound manner.
It handles method calls and property accesses through the Invoke
method of the IDispatch
interface, mapping COM calls to .NET method invocations and vice-versa.
Members
-
ConstructorInitializes a new instance of the
IDispatchImpl
class. -
Invoke(DISPID dispIdMember, Guid& riid, LCID lcid, WFANG flags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)Dispatches an invocation call. This is the core method for handling COM calls to .NET objects. It translates COM method calls and property accesses into .NET method calls or property get/set operations.
-
GetTypeInfoCount()Retrieves the number of type information interfaces supported by the object.
-
GetTypeInfo(UINT iTInfo, LCID lcid, IntPtr& ppTInfo)Retrieves type information for the object.
-
GetIDsOfNames(Guid& riid, IntPtr rgszNames, UINT cNames, LCID lcid, DISPID* pDispIds)Maps an array of names to an array of dispatch identifiers (DISPIDs).
Remarks
The IDispatchImpl
class is primarily used internally by the .NET Framework to facilitate COM interop. Developers typically do not need to instantiate or directly interact with this class. Instead, they define .NET classes that can be exposed to COM, and the framework handles the COM marshaling and the implementation of IDispatch
.
Key aspects:
- Late Binding:
IDispatch
enables late binding, allowing COM clients to discover and invoke members of an object at runtime without prior knowledge of its exact type. - Type Information: The methods
GetTypeInfoCount
andGetTypeInfo
are used by COM clients to obtain metadata about the object, allowing for introspection and dynamic member access. - DISPID: Dispatch identifiers (DISPIDs) are unique identifiers for members (methods, properties, events) of a COM object.
GetIDsOfNames
maps member names to their corresponding DISPIDs.
Example
While direct usage is uncommon, consider a scenario where you have a .NET class exposed to COM. The IDispatchImpl
is part of the underlying machinery that makes this work.
// Example of a .NET class intended for COM interop
[ComVisible(true)]
[Guid("YOUR-UNIQUE-GUID-HERE")]
public class MyComVisibleClass
{
public string SayHello(string name)
{
return $"Hello, {name}!";
}
public int MyProperty { get; set; }
}
// When a COM client accesses MyComVisibleClass, the .NET runtime
// uses an implementation akin to IDispatchImpl to handle the calls
// through IDispatch. For instance, a call like:
// obj.SayHello("World")
// will be marshaled by the runtime using IDispatch.Invoke.