private static Func <Type, string, string, Exception> CreateExceptionCreator() { var dynamicMethod = new DynamicMethod( "__srpc__CreateException", typeof(Exception), new[] { typeof(Type), typeof(string), typeof(string) }, Assembly.GetExecutingAssembly().ManifestModule, true); var il = new MyILGenerator(dynamicMethod.GetILGenerator()); il.Ldarg(0); // stack_0 = (Exception)FormatterServices.GetUninitializedObject(typeof(T)) il.Call(GetUninitializedObject); il.Castclass(typeof(Exception)); il.Dup(); // stack_0.Init() il.Call(InitMethod); il.Dup(); // stack_0._message = message il.Ldarg(1); il.Stfld(MessageField); il.Dup(); // stack_0._message = message il.Ldarg(2); il.Stfld(RemoteStackTraceStringField); il.Ret(); return((Func <Type, string, string, Exception>)dynamicMethod.CreateDelegate(typeof(Func <Type, string, string, Exception>))); }