要用C#反射技术的话,首先得引入System.Reflection 命名空间,这个命名空间里的类,具有动态加载程序集、类型,动态调用方法、设置和取得属性和字段的值、可以获取类型和方法的信息的功能。 要想对一个类型实例的属性或字段进行动态赋值或取值,首先得得到这个实例或类型的Type,微软已经为我们提供了足够多的方法。
1
Class MyClass
2
{
3
private
int
field;
4
public
int
Field
5
{
6
get
7
{
8
return
this
.field;
9
}
10
set
11
{
12
this
.field
=
value;
13
}
14
}
15
}
如果有个这个类型的实例:MyClass myObj = new MyClass(); 我们要动态的为这个实例的属性 Field赋值,那么得先得到这个实例的类型:
Type t
=
typeof
(MyClass);
另一种方法是:
Type t
=
myObj.GetType();
只要我们得到了对象的类型那么我们就可以利用反射对这个对象“为所欲为”了,哈哈。
t.GetProperty(
"
Field
"
).SetValue(myObj,
1
,
null
);
这样我们就为对象里的属性Field赋值了。如果把属性名和要赋的值写道配置文件里的话,我们就可以达到程序运行期间动态的为属性赋值了。 利用反射获取属性值的方法:
int
propValue
=
Convert.ToInt32(t.GetProperty(
"
Field
"
).SetValue(myObj,
null
));
好了,关于利用反射动态的为对象的属性赋值取值就到这里了。
(C# )利用反射动态调用类成员 使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下:public object InvokeMember( string name, BindingFlags invokeAttr, Binder binder , object target, object [ ] args) ; 参数 name String,它包含要调用的构造函数、方法、属性或字段成员的名称。 - 或 - 空字符串 ( "" ) ,表示调用默认成员。 invokeAttr 一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。访问可以是 BindingFlags 之一,如 Public、NonPublic、Private、InvokeMethod 和 GetField 等。不需要指定查找类型。如果省略查找类型,则将应用 BindingFlags . Public | BindingFlags . Instance。 binder 一个 Binder 对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。 - 或 - 若为空引用(Visual Basic 中为 Nothing),则使用 DefaultBinder。 target 要在其上调用指定成员的 Object。 args 包含传递给要调用的成员的参数的数组。 返回值 表示被调用成员的返回值的 Object。 备注: 下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags . Instance 或 BindingFlags . Static。 指定 BindingFlags . Public 可在搜索中包含公共成员。 指定 BindingFlags . NonPublic 可在搜索中包含非公共成员(即私有成员和受保护的成员)。 指定 BindingFlags . FlattenHierarchy 可包含层次结构上的静态成员。 下列 BindingFlags 修饰符标志可用于更改搜索的执行方式: BindingFlags . IgnoreCase,表示忽略 name 的大小写。 BindingFlags . DeclaredOnly,仅搜索 Type 上声明的成员,而不搜索被简单继承的成员。 可以使用下列 BindingFlags 调用标志表示要对成员采取的操作: CreateInstance,表示调用构造函数。忽略 name。对其他调用标志无效。 InvokeMethod,表示调用方法,而不调用构造函数或类型初始值设定项。对 SetField 或 SetProperty 无效。 GetField,表示获取字段值。对 SetField 无效。 SetField,表示设置字段值。对 GetField 无效。 GetProperty,表示获取属性。对 SetProperty 无效。 SetProperty 表示设置属性。对 GetProperty 无效。 下面通过例题对该方法进行简单应用(我一直以为,要让例题起到更容易理解文字的意义和作用,撰写的例题越简单越直观越好。)using System ; using System . Reflection ; namespace ConsoleApplication9{ class Love { public int field1; private string _name; public Love( ) { } public string Name { get { return _name; } set { _name= value; } } public int GetInt( int a) { return a; } public void Display( string str) { Console . WriteLine( str) ; } } /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { /// <summary> /// 应用程序的主入口点。 /// </summary> [ STAThread] static void Main( string [ ] args) { // // TODO: 在此处添加代码以启动应用程序 // Love love = new Love( ) ; Type type = love. GetType( ) ; Object obj = type . InvokeMember( null , BindingFlags . DeclaredOnly | BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . CreateInstance, null , null , args) ; //调用没有返回值的方法 type . InvokeMember( "Display" , BindingFlags . InvokeMethod | BindingFlags . Public | BindingFlags . Instance , null , obj , new object [ ] { "aldfjdlf" } ) ; //调用有返回值的方法 int i = ( int ) type . InvokeMember( "GetInt" , BindingFlags . InvokeMethod | BindingFlags . Public | BindingFlags . Instance , null , obj, new object [ ] { 1} ) ; Console . WriteLine( i) ; //设置属性值 type . InvokeMember( "Name" , BindingFlags . SetProperty, null , obj, new string [ ] { "abc" } ) ; //获取属性值 string str= ( string ) type . InvokeMember( "Name" , BindingFlags . GetProperty, null , obj, null ) ; Console . WriteLine( str) ; //设置字段值 type . InvokeMember( "field1" , BindingFlags . SetField, null , obj, new object [ ] { 444} ) ; //获取字段值 int f= ( int ) type . InvokeMember( "field1" , BindingFlags . GetField, null , obj, null ) ; Console . WriteLine( f) ; Console . ReadLine( ) ; } } }