代码改变世界

反射(转载)

2010-11-26 13:52  爱研究源码的javaer  阅读(259)  评论(0编辑  收藏  举报
using System;
using System.Reflection;

namespace DynamicCall
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public static void Main(string[] args)
{
Calc calc;

string typeName = typeof (Calc).FullName;

Type type = null;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
type = assembly.GetType(typeName);
if (type != null)
{
break;
}
}

if (type != null)
{
calc = (Calc) type.Assembly.CreateInstance(typeName);
}
else
{
return;
}

BindingFlags flags;

//设置Name属性
flags = BindingFlags.SetProperty;
type.InvokeMember("Name", flags, null, calc, new object[] {"My name is wql"});

//获得Name属性
flags = BindingFlags.GetProperty;
string name = (string) type.InvokeMember("Name", flags, null, calc, null);
Console.WriteLine(name);

//增加事件
flags = BindingFlags.InvokeMethod;
OnAddEvent eventProc = new OnAddEvent(calc_AfterAdd);
type.InvokeMember("add_AfterAdd", flags, null, calc, new object[] {eventProc});

//调用Add方法
int a = 3;
int b = 2;
flags = BindingFlags.InvokeMethod;
type.InvokeMember("Add", flags, null, calc, new object[] {a, b});

//calc["Name"] = "zyn";
//Console.WriteLine(calc.Name);

Console.WriteLine(GetProperty(calc, "Name"));
}

private static void calc_AfterAdd(int a, int b, int result)
{
Console.WriteLine("calc_AfterAdd:a={0},b={1},result={2}", a, b, result);
}

public static object GetProperty(object obj, string propName)
{
BindingFlags flags = BindingFlags.GetProperty;
return obj.GetType().InvokeMember(propName, flags, null, obj, null);
}
}

public delegate void OnAddEvent(int a, int b, int result);

public class Calc
{
private string name;
private int count = 0;

// public string this[string propName]
// {
// get
// {
// BindingFlags flags = BindingFlags.GetProperty;
// string value =(string) this.GetType().InvokeMember(propName, flags, null, this, null);
// return value;
// }
// set
// {
// BindingFlags flags = BindingFlags.SetProperty;
// this.GetType().InvokeMember(propName, flags, null, this, new object[]{value});
// }
// }

public int Add(int a, int b)
{
count ++;
int result = a + b;
this.FireOnAddEvent(a, b, result);
return result;
}

public int Count
{
get
{
return this.count;
}
}

public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}

public event OnAddEvent AfterAdd;

private void FireOnAddEvent(int a, int b, int result)
{
if (this.AfterAdd != null)
{
this.AfterAdd(a, b, result);
}
}

public override string ToString()
{
return string.Format("{0}[{1}]", this.GetType().FullName, this.name);
}

}
}
[/code]
2
[code]
using System;

namespace MyClassLib
{
class MyClass{
public int a;

public int GetSum(int x, int y) {
return x + y + a;
}

}
}

class ReflectionHelper {

public static object SetValue(string p_ClassName, string p_FieldName, string p_FieldValue) {
// 加载程序集
Assembly _Assembly = Assembly.LoadFrom("MyClassLib.dll");

// 加载类型
Type _Type = _Assembly.GetType(p_ClassName);

// 创建实例
Object _Object = System.Activator.CreateInstance(_Type);

// 取属性
FieldInfo _FieldInfo = _Type.GetField(p_FieldName);

// 类型转换
Object _Value = System.Convert.ChangeType(p_FieldValue, _FieldInfo.FieldType);

// 设值
_FieldInfo.SetValue(_Object, _Value);

#region 方法调用
// 给参数
System.Type[] _ParamTypes = new System.Type[2];
_ParamTypes[0] = System.Type.GetType("System.Int32");
_ParamTypes[1] = System.Type.GetType("System.Int32");

// 取方法信息
MethodInfo _MethodInfo = _Type.GetMethod("GetSum", _ParamTypes);

// 参数值
Object[] _Params = new Object[2];
_Params[0] = 3;
_Params[1] = 4;

//
Object _ReturnValue = _MethodInfo.Invoke(_Object, _Params);

Console.WriteLine("MyClassLib.MyClass.GetSum(3, 4) returns: {0}", _ReturnValue.ToString());
#endregion

return _Object;
}

static void Main() {
Object o = SetValue("MyClassLib.MyClass","a","12");
Console.WriteLine(o.GetType().Name);
Console.WriteLine(o.GetType().GetField("a").GetValue(o));
Console.WriteLine(o.GetType().GetField("a").GetValue(o).GetType().Name);
Console.ReadLine();
}
}转自:http://devil.bokee.com/5382871.html