首页 新闻 会员 周边 捐助

反射高手进高手都不是看豆来的把本人豆豆有点少

0
悬赏园豆:5 [已解决问题] 解决于 2014-09-25 15:25
委托泛型高性能反射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也没有看懂是错在那里 有大神看到麻烦 给个解答!

Demo

苦逼面条的主页 苦逼面条 | 菜鸟二级 | 园豆:289
提问于:2012-04-20 09:31
< >
分享
最佳答案
0

以上代码有个BUG 传递 int 类型参数会有参数错误的现象

你的代码和demo都运行正常。解释下是在哪里传递int类型的参数?参数的值是多少?什么叫“参数错误的现象”?抛异常吗?异常的信息又是什么?

收获园豆:5
水牛刀刀 | 大侠五级 |园豆:6350 | 2012-04-20 10:35
  //int _a = 0;
        //int _b = 0;
        //public sum(int a, int b)
        //{
        //    this._a = a;
        //    this._b = b;

        //}
        //public void add()
        //{
        //    Console.WriteLine(_a + _b);
        //}

你去把这里取消注释把上面的 string 类型的注释掉 然后在下面调用

  object[] args1 = new object[] { 11, 22 };
           Type[] argTypes = new Type[]{
           typeof(int),typeof(int
           )
           };
           New<sum>(typeof(sum), argTypes, args1).add();

 你会发现传进来的参数不是11  和 22 会是很大的数字 比如  4223232323 什么的

苦逼面条 | 园豆:289 (菜鸟二级) | 2012-04-20 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册