Table of Contents

Class EmitDllMappableClass

Namespace
Utils.Reflection.Reflection.Emit
Assembly
Utils.Reflection.dll

Utility class for dynamically generating types that map to DLL functions. This class emits new types at runtime, which implement a specified interface. The methods of the interface are mapped to corresponding unmanaged functions using the DllMapper class.

public static class EmitDllMappableClass
Inheritance
EmitDllMappableClass
Inherited Members

Remarks

Security warning: the generated C# source is built by concatenating type, method and parameter names obtained through reflection on the target interface. CLR metadata names are far less constrained than C# lexical identifiers, so an interface sourced from an untrusted or dynamically generated assembly could inject arbitrary C# into the compiled/loaded assembly. Only call these methods with interfaces you fully trust. Emit<TInterface>(string, CallingConvention, TimeSpan?, TimeSpan?) performs the same generation inside an isolated worker process instead, which contains that risk.

Methods

Emit(Type, CallingConvention)

Emits a class that implements the specified interface and maps the interface methods to DLL functions.

[Experimental("UTILSREFL001")]
public static object Emit(Type type, CallingConvention callingConvention = CallingConvention.Winapi)

Parameters

type Type

Interface type

callingConvention CallingConvention

The calling convention of the unmanaged functions.

Returns

object

An instance of the dynamically generated class

Exceptions

NotSupportedException

Thrown when type is not an interface, or is a generic interface, or declares a generic method.

Emit<TInterface>(CallingConvention)

Emits a class that implements the specified interface and maps the interface methods to DLL functions. Each delegate gets an UnmanagedFunctionPointerAttribute carrying the requested convention.

[Experimental("UTILSREFL001")]
public static TInterface Emit<TInterface>(CallingConvention callingConvention) where TInterface : class

Parameters

callingConvention CallingConvention

The calling convention of the unmanaged functions.

Returns

TInterface

An instance of the dynamically generated class

Type Parameters

TInterface

Interface type