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
static
void
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
public
delegate
void
CheckAnimal
<
T
>
(T t);
30
31
32
///
<summary>
33
///
检查对象属性
34
///
</summary>
35
///
<param name="tiger"></param>
36
public
static
void
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
public
static
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
public
static
void
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