看了老赵快速反射顺便提到了一个DynamicUtilites反射工具,我就举个小例子说明下怎么使用它

由于传统的反射性能不是很好,所以许多高手都想尽方法提高反射的性能

以下DynamicUtilites编是其中一个(和.net4.0中的那个Dynamic反射不同)

 

首先在项目中添加DynamicUtilites项目

DynamicUtilites项目下有三个文件 

Dynamic.cs

DynamicComparer.cs

DynamicEmit.cs

 

如果由于精力有限无法了解该反射辅助类的具体实现原理,请大家原谅 ^^,大致是将原有反射代码的封装和优化,有空我会去了解下该辅助类的源码

 

以下简单介绍如何使用该反射类

 

先建立一个ICallable接口,注意是泛型接口s

 

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Text;
4
5  namespace DynamicTest
6 {
7 publicinterface ICallable<T>
8 {
9 double Age { set;get; }
10 string Gender { set;get; }
11 string Name { set;get;}
12 T Create(string name, string gender);
13 string Eat(T t);
14 //string Dead(T t);
15  string Sleep(T t,string time);
16 }
17 }
18  

 

然后编写一个老虎类实现该接口

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Text;
4
5  namespace DynamicTest
6 {
7 publicclass Tiger:ICallable<Tiger>
8 {
9 public Tiger() { }
10 public Tiger(double age, string name, string gender)
11 {
12 this.age = age;
13 this.gender = gender;
14 this.name = name;
15
16 }
17
18 private Rabbit rabbit;
19 public Rabbit Rabbit
20 {
21 get { return rabbit; }
22 set { value = rabbit; }
23 }
24
25 privatedouble age=0;
26 publicdouble Age
27 {
28 get { return age; }
29 set { value = age; }
30 }
31 privatestring gender;
32 publicstring Gender
33 {
34 get { return gender; }
35 set { value = gender; }
36 }
37
38 privatestring name;
39 publicstring Name
40 {
41 get { return name; }
42 set { value = name; }
43 }
44
45 public Tiger Create(string name, string gender)
46 {
47 thrownew Exception("The method or operation is not implemented.");
48 }
49
50
51
52
53 publicstring Sleep(Tiger t, string time)
54 {
55 return"the time is "+time+" ,the tiger witch name is "+ t.Name +" is sleeping!";
56 }
57
58 publicstring Eat(Tiger t)
59 {
60 return t.Name+" is eating";
61 }
62
63
64 }
65
66
67
68
69 publicclass Rabbit
70 {
71 privatestring name;
72 publicstring Name
73 {
74 get { return name; }
75 set { value = name; }
76 }
77
78
79 public Rabbit(string name)
80 {
81 this.name = name;
82 }
83 }
84 }
85  

 

 

 

真正的客户端反射类的实现,请大家仔细看注释,

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Text;
4  //using System.Reflection;
5  using Phydeaux.Utilities;
6  using System.Reflection;
7
8  namespace DynamicTest
9 {
10 /*
11 * 该类是实现客户端反射,思路是先反射出老虎类对象
12 * 然后检查该对象属性是否符合条件,并且通过反射改变
13 * 实体属性值,加以显示
14 *
15 *
16 */
17 class Program
18 {
19 staticvoid Main(string[] args)
20 {
21 ActionMethod<Tiger>(Check, 99, "逆时针の风", "male");
22 }
23
24 ///<summary>
25 /// 一个委托
26 ///</summary>
27 ///<typeparam name="T"></typeparam>
28 ///<param name="t"></param>
29 publicdelegatevoid CheckAnimal<T>(T t);
30
31
32 ///<summary>
33 /// 检查对象属性
34 ///</summary>
35 ///<param name="tiger"></param>
36 publicstaticvoid Check(Tiger tiger)
37 {
38 if (string.IsNullOrEmpty(tiger.Name))
39 {
40 tiger.Name ="逆时针の风";
41 }
42 if (string.IsNullOrEmpty(tiger.Gender))
43 {
44 tiger.Gender ="male";
45 }
46 if (tiger.Age ==0)
47 {
48 tiger.Age =99;
49 }
50 }
51
52 ///<summary>
53 /// 利用构造器对象反射出T对象t实例
54 ///</summary>
55 ///<typeparam name="T">对象</typeparam>
56 ///<param name="con">构造器对象</param>
57 ///<param name="age"></param>
58 ///<param name="name"></param>
59 ///<param name="gender"></param>
60 ///<returns></returns>
61 publicstatic T CreateObject<T>(Constructor<T, double, string, string> con, double age, string name, string gender) where T : ICallable<T>
62 {
63 T t = (T)con.Invoke(age, name, gender);
64 return t;
65 }
66
67 ///<summary>
68 /// 利用Dnamic发射获取相关对象和行为
69 ///</summary>
70 ///<typeparam name="T">对象</typeparam>
71 ///<param name="checkAnimal">checkAnimal委托,在Main(string[] args)方法中直接用方法Check填入,注意用方法名即可</param>
72 ///<param name="age"></param>
73 ///<param name="name"></param>
74 ///<param name="gender"></param>
75 publicstaticvoid ActionMethod<T>(CheckAnimal<T> checkAnimal, double age, string name, string gender) where T : ICallable<T>, new()
76 {
77 /*先建立构造器对象,因为tiger类的构造函数参数是(double age, string name, string gender)
78 * 所以Explicit<double, string, string>也必须一致,构造器对象的目的是使用Invoke方法反射获取该对象
79 */
80 Constructor<T, double, string, string> create = Dynamic<T>.Constructor.Explicit<double, string, string>.CreateDelegate(ParameterList.Auto);
81 /*
82 * 这里比较抽象,产生animal_EatAction委托找出tiger类的eat方法
83 * 需要注意的是Explicit<T>的T参数决定于eat方法的参数个数和类型
84 *
85 */
86 Func<T, string, T> animal_EatAction = Dynamic<T>.Instance.Function<string>.Explicit<T>.CreateDelegate("Eat");
87
88 /*
89 * 同样利用Dynamic中的自定义委托Proc接受属性的写反射
90 *
91 */
92 Proc<T, double> SetAge = Dynamic<T>.Instance.Field<double>.Setter.CreateDelegate("age");
93 /*
94 * 同样利用Dynamic中的委托Func接受属性的读反射
95 *
96 */
97 Func<T, double> GetAge = Dynamic<T>.Instance.Field<double>.Getter.CreateDelegate("age");
98
99 //先获取t对象实例
100 T t = CreateObject<T>(create, age, name, gender);
101 //获取该类年龄属性值
102 double originAge = GetAge(t);
103 //更改该类年龄属性值
104 SetAge(t,100);
105
106 //执行各种属性检查
107 checkAnimal(t);
108 //执行t对象的eat方法
109 Console.WriteLine(animal_EatAction(t, t)+" the age of this tiger is "+t.Age+"!");
110 Console.ReadLine();
111 }
112
113 }
114 }
115

 

 

以上反射的性能肯定比.net自带的 强一点,该辅助类下载地址暂时找不到,请大家谅解,地址放在公司电脑里了。。过几天发布地址

 

谢谢!

posted @ 2010-06-14 17:58  逆时针の风  阅读(1562)  评论(1编辑  收藏  举报