using System; using System.Reflection.Emit; namespace ZeroLevel.Services.Reflection { public static class ReferenceHelpers { public static readonly Action> GetPinnedPtr; static ReferenceHelpers() { var dyn = new DynamicMethod("GetPinnedPtr", typeof(void), new[] { typeof(object), typeof(Action) }, typeof(ReferenceHelpers).Module); var il = dyn.GetILGenerator(); il.DeclareLocal(typeof(object), true); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Stloc_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldloc_0); il.Emit(OpCodes.Conv_I); il.Emit(OpCodes.Call, typeof(Action).GetMethod("Invoke")); il.Emit(OpCodes.Ret); GetPinnedPtr = (Action>)dyn.CreateDelegate(typeof(Action>)); } } }