Table of Contents

Class ComDynamic

Namespace
CsCom.Runtime
Assembly
CsCom.Runtime.Dynamic.dll

Gives late-bound access to COM objects through dynamic.

public static class ComDynamic
Inheritance
ComDynamic
Inherited Members

Remarks

A late-bound wrapper or a dynamic object stands for its COM object. An object of the application that is exposed to COM ([GeneratedComClass]) is late bound itself when it implements IDispatch, also when it implements IDispatchProvider, and otherwise through the Dispatch that it provides, also for its events.

Extension members of ComActivator

ComDynamic also declares static extension members of ComActivator (C# 14), which are called on ComActivator, e.g. ComActivator.CreateDynamicInstance("Excel.Application"). All of them require dynamic code ([RequiresDynamicCode]) and throw an InvalidCastException when the object does not implement IDispatch.

Member Description
CreateDynamicInstance(Guid clsid, ClassContext context = ComActivator.DefaultContext) Creates an instance of the COM class identified by clsid. context is the ClassContext in which the code that manages the new object runs.
CreateDynamicInstance(string progId, ClassContext context = ComActivator.DefaultContext) Creates an instance of the COM class identified by its ProgID, e.g. Excel.Application.
GetActiveDynamicInstance(Guid clsid)
GetActiveDynamicInstance(string progId)
Gets the running instance of the class from the running object table. Throws a COMException (MK_E_UNAVAILABLE) when no instance is registered.
TryGetActiveDynamicInstance(Guid clsid, out dynamic? instance)
TryGetActiveDynamicInstance(string progId, out dynamic? instance)
Gets the running instance of the class if one is registered in the running object table; returns false otherwise, also when the ProgID is not registered.
BindToDynamicMoniker(string name) Gets the object identified by a moniker display name, like VB's GetObject: the path of a document, winmgmts:, … Throws a COMException when the name is not a valid moniker or the object cannot be bound.

See Late binding with dynamic.

Methods

AddEventHandler(object, string, Delegate)

Adds a handler of an event of a COM object, like d.Event += handler, also for events whose names are those of methods or properties of the object.

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static void AddEventHandler(object comObject, string eventName, Delegate handler)

Parameters

comObject object

A COM object that implements IDispatch, a late-bound wrapper or a dynamic object, or an System.Runtime.InteropServices.UnknownWrapper of one; a System.Runtime.InteropServices.DispatchWrapper is rejected with an ArgumentException.

eventName string

The name of a method of a source dispinterface of the coclass of the object.

handler Delegate

The handler: a delegate with at most the parameters of the event, which receives the arguments converted to the types of its parameters, and returns the values of ref and out parameters and the result of the event.

Exceptions

ArgumentException

The object has no such event, or the handler has more parameters than the event.

InvalidOperationException

The object does not raise the events of the source dispinterface.

COMException

Connecting to the object failed with another error, e.g. of a server that has ended or is busy.

AsDynamic(DispatchObject)

Returns the COM object of a late-bound dispinterface wrapper for late binding with dynamic, like ToDynamic(object?): a wrapper of the application that is exposed to COM and implements IDispatch is late bound itself.

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static dynamic AsDynamic(this DispatchObject wrapper)

Parameters

wrapper DispatchObject

The late-bound wrapper.

Returns

dynamic

AsDynamic(IDispatch)

Returns the COM object for late binding with dynamic, like ToDynamic(object?).

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static dynamic AsDynamic(this IDispatch dispatch)

Parameters

dispatch IDispatch

The IDispatch interface of the object, or a dual interface derived from it.

Returns

dynamic

GetTypeName(object)

Gets the name of the type of a COM object from its type information, like VB's TypeName, e.g. the name of the default interface of its class.

public static string? GetTypeName(object comObject)

Parameters

comObject object

A COM object that implements IDispatch, a late-bound wrapper or a dynamic object, or an System.Runtime.InteropServices.UnknownWrapper of one.

Returns

string

The name, or null if the object provides no type information.

Exceptions

InvalidCastException

The object does not implement IDispatch.

ArgumentException

The object is a System.Runtime.InteropServices.DispatchWrapper.

RemoveEventHandler(object, string, Delegate)

Removes a handler of an event of a COM object, like d.Event -= handler. The connection to the source dispinterface is closed when it has no handlers left.

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static void RemoveEventHandler(object comObject, string eventName, Delegate handler)

Parameters

comObject object

A COM object that implements IDispatch, a late-bound wrapper or a dynamic object, or an System.Runtime.InteropServices.UnknownWrapper of one; a System.Runtime.InteropServices.DispatchWrapper is rejected with an ArgumentException.

eventName string

The name of the event.

handler Delegate

The handler to remove.

Exceptions

ArgumentException

The object has no such event.

RemoveEventHandlers(object)

Removes the handlers of all events of a COM object added through dynamic and closes the connections to its source dispinterfaces.

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static void RemoveEventHandlers(object comObject)

Parameters

comObject object

A COM object that implements IDispatch, a late-bound wrapper or a dynamic object, or an System.Runtime.InteropServices.UnknownWrapper of one; a System.Runtime.InteropServices.DispatchWrapper is rejected with an ArgumentException.

ToDynamic(object?)

Returns a COM object, e.g. one returned in a VARIANT, for late binding with dynamic.

[RequiresDynamicCode("Late binding with dynamic requires dynamic code generation and is not compatible with Native AOT.")]
public static dynamic? ToDynamic(object? comObject)

Parameters

comObject object

A COM object that implements IDispatch, a late-bound wrapper, or null; a late-bound wrapper whose Dispatch is null, against its contract, is converted to null too. An System.Runtime.InteropServices.UnknownWrapper stands for its object, as where an object is passed to COM.

Returns

dynamic

Exceptions

InvalidCastException

The object does not implement IDispatch.

ArgumentException

The object is a System.Runtime.InteropServices.DispatchWrapper.