static public void Prepare(this MethodBase method, bool needSpecialPrepare = false) { // test not good enough (too broad) //if (method.DeclaringType != null && method.DeclaringType.IsGenericType) // todo Jens maybe better but still not good enough //if (needSpecialPrepare) //{ // // e.g.: if method has not been called yet // // Jens maybe also need to test if method type parameter unused but how can our code see that? // var _runtimeTypeHandles = method.DeclaringType.GetGenericArguments().Select(type => type.TypeHandle).ToArray(); // RuntimeHelpers.PrepareMethod(method.Handle(), _runtimeTypeHandles); // return; //} try { RuntimeHelpers.PrepareMethod(method.Handle()); } catch (ArgumentException e) //when (e.Message == "The given generic instantiation was invalid.") { // we know what to do just not when to do it (supplying TypeHandles when not needed also course an exception) // see also https://stackoverflow.com/questions/31754418/runtimehelpers-preparemethod-not-working-when-called-with-funcstring-created-i var _runtimeTypeHandles = method.DeclaringType.GetGenericArguments().Select(type => type.TypeHandle).ToArray(); RuntimeHelpers.PrepareMethod(method.Handle(), _runtimeTypeHandles); } }
static public IntPtr Pointer(this MethodBase method) { return(method.Handle().GetFunctionPointer()); }
static public void Prepare(this MethodBase method) { RuntimeHelpers.PrepareMethod(method.Handle()); }