/// <summary> /// Creates a wrapper function that transforms our own C# function into an arbitrary calling convention; with pointer to transformed C# function. /// Basically, this creates a function pointer to a C# function. /// Note: You must keep an instance of this class as long as you're using the function pointer. /// </summary> /// <param name="function">The function to create a function pointer to.</param> public static ReverseFunctionWrapper <TFunction> CreateReverseWrapper(TFunction function) { // Set our C# function. var reverseFunctionWrapper = new ReverseFunctionWrapper <TFunction>(); reverseFunctionWrapper.CSharpFunctionCopy = function; // Take fast path for CDECL, otherwise assemble wrapper. var reloadedFunctionAttribute = FunctionCommon.GetReloadedFunctionAttribute <TFunction>(); if (reloadedFunctionAttribute.Equals(new ReloadedFunctionAttribute(CallingConventions.Cdecl))) { reverseFunctionWrapper.Pointer = (IntPtr)Marshal.GetFunctionPointerForDelegate(function); } else { reverseFunctionWrapper.Pointer = CreateReverseWrapperInternal <TFunction>(Marshal.GetFunctionPointerForDelegate(function), reloadedFunctionAttribute); } return(reverseFunctionWrapper); }