高性能反射初体验4
委托泛型高性能反射View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Reflection; 6 using System.Reflection.Emit; 7 8 namespace Reflx_Method 9 { 10 class sum 11 { 12 string _a = ""; 13 string _b = ""; 14 public sum(string a, string b) 15 { 16 this._a = a; 17 this._b = b; 18 19 } 20 public void add() 21 { 22 Console.WriteLine(_a + _b); 23 } 24 //int _a = 0; 25 //int _b = 0; 26 //public sum(int a, int b) 27 //{ 28 // this._a = a; 29 // this._b = b; 30 31 //} 32 //public void add() 33 //{ 34 // Console.WriteLine(_a + _b); 35 //} 36 } 37 class Program 38 { 39 public delegate TReturn InstanceHandlerProxy<TReturn>(object[] args); 40 static void Main(string[] args) 41 { 42 43 44 object[] args1 = new object[] { "11", " 22" }; 45 Type[] argTypes = new Type[]{ 46 typeof(string),typeof(string 47 ) 48 }; 49 New<sum>(typeof(sum), argTypes, args1).add(); 50 51 52 } 53 public static TReturn New<TReturn>(Type objType, Type[] argTypes, object[] args) 54 { 55 //return GetDelegate<TReturn>(objType, argTypes)(args); 56 InstanceHandlerProxy<TReturn> invoker = GetDelegate<TReturn>(objType, argTypes); 57 //IList<LocalVariableInfo> varibles =invoker.Method.GetMethodBody().LocalVariables; 58 return invoker.Invoke(args); 59 } 60 61 //public static sum DTFDynamicFactory(object[] objArray1) 62 //{ 63 // object[] objArray = objArray1; 64 // string a = (string)objArray[0]; 65 // return new sum(a, (string)objArray[1]); 66 //} 67 68 private static InstanceHandlerProxy<TReturn> GetDelegate<TReturn>(Type objType, Type[] argTypes) 69 { 70 DynamicMethod dynamicMethod = new DynamicMethod("DTFDynamicFactory", objType, new Type[] { typeof(object[]) }, typeof(Program).Module); 71 //{ 72 // InitLocals=false 73 //}; 74 ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); 75 int argNum = argTypes.Length; 76 LocalBuilder objects = ilGenerator.DeclareLocal(typeof(object[])); 77 ilGenerator.Emit(OpCodes.Ldarg_0); 78 ilGenerator.Emit(OpCodes.Stloc, objects); 79 LocalBuilder returnObject = ilGenerator.DeclareLocal(objType); 80 81 LocalBuilder[] var = new LocalBuilder[argNum]; 82 for (int i = 0; i < argNum; i++) 83 { 84 var[i] = ilGenerator.DeclareLocal(argTypes[i]); 85 ilGenerator.Emit(OpCodes.Ldloc_0); 86 ilGenerator.Emit(OpCodes.Ldc_I4_S, Convert.ToByte(i)); 87 //ilGenerator.Emit(OpCodes.Ldelem_Ref); 88 ilGenerator.Emit(OpCodes.Ldelem_Ref); 89 ilGenerator.Emit(OpCodes.Stloc, var[i]); 90 } 91 92 for (int i = 0; i < var.Length; i++) 93 { 94 95 ilGenerator.Emit(OpCodes.Ldloc, var[i]); 96 } 97 98 Type overflow = objType; 99 ConstructorInfo exCtorInfo = overflow.GetConstructor(argTypes); 100 101 ilGenerator.Emit(OpCodes.Newobj, exCtorInfo); 102 ilGenerator.Emit(OpCodes.Stloc_1); 103 ilGenerator.Emit(OpCodes.Ldloc_1); 104 ilGenerator.Emit(OpCodes.Ret); 105 return (InstanceHandlerProxy<TReturn>)dynamicMethod.CreateDelegate(typeof(InstanceHandlerProxy<TReturn>)); 106 } 107 } 108 109 }
等效代码View Code
1 public sum DTFDynamicFactory(object[]) 2 { 3 object[] objArray = (object[]) this; 4 string a = (string) objArray[0]; 5 return new sum(a, (string) objArray[1]); 6 }
以上代码有个BUG 传递 int 类型参数会有参数错误的现象 我初学IL也没有看懂是错在那里 有大神看到麻烦 给个解答!