1.用反射動態創建類實例,並調用其公有成員函數。
//新建一個類庫項目,增加一個GetSum方法。
using System;
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
public int GetSum(int x, int y)
{
return x + y;
}
}
}
//再另建一個項目,在項目中引用上面生成的ClassLibrary1.DLL
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom("ClassLibrary1.DLL");
System.Type t = a.GetType("ClassLibrary1.Class1");
//動態生成ClassLibrary1.Class類的實例
Object theObj = System.Activator.CreateInstance(t);
//參數信息,GetSum需要兩個int參數
System.Type[] paramTypes = new System.Type[2];
paramTypes[0] = System.Type.GetType("System.Int32");
paramTypes[1] = System.Type.GetType("System.Int32");
System.Reflection.MethodInfo mi = t.GetMethod("GetSum", paramTypes);
//參數值
Object[] parameters = new Object[2];
parameters[0] = 3;
parameters[1] = 4;
Object returnValue = mi.Invoke(theObj, parameters);
Console.WriteLine("ClassLibrary1.Class1.GetSum(3, 4) returns: {0}", returnValue.ToString());
2.用反射訪問類的私有成員。
如果是C++,我們可以計算對象內成員的位置,然後偏移指針以訪問類型的所有非公開成員。但是.NET對象完全受GC管理,地址根本無法得到,並且也無法通過指針調用方法。
當然... 這是一種很不值得推薦的技巧,訪問非公有成員很可能破壞對象狀態,造成不可預料的後果。但是無論如何,利用.NET的反射機制可以做到。
比如這樣一個類:
class MyClass
{
private string PrivateField = "Private Field";
protected string ProtectedField = "Protected Field";
private string _ProtectedProperty = "Protected Property";
protected string ProtectedProperty
{
get{return _ProtectedProperty;}
set{_ProtectedProperty = value;}
}
private string _PrivateProperty = "Private Property";
private string PrivateProperty
{
get{return _PrivateProperty;}
set{_PrivateProperty = value;}
}
protected void ProtectedMethod()
{
Console.WriteLine("Protected Method Invoked");
}
private void PrivateMethod()
{
Console.WriteLine("Private Method Invoked");
}
}
除了默認的構造函數,沒有任何成員是公開的,但是我仍然想獲取和設置Field和Property的值,以及調用那兩個方法。方法是:
MyClass mc = new MyClass();
Type t = typeof(MyClass);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;
// Fields
FieldInfo fi_protected = t.GetField("ProtectedField",bf);
FieldInfo fi_private = t.GetField("PrivateField",bf);
Console.WriteLine(fi_protected.GetValue(mc));
Console.WriteLine(fi_private.GetValue(mc));
fi_private.SetValue(mc,"New Private Field");
Console.WriteLine(fi_private.GetValue(mc));
Console.WriteLine();
// Properties
PropertyInfo pi_protected = t.GetProperty("ProtectedProperty", bf);
PropertyInfo pi_private = t.GetProperty("PrivateProperty", bf);
Console.WriteLine(pi_protected.GetValue(mc,null));
Console.WriteLine(pi_private.GetValue(mc,null));
pi_private.SetValue(mc,"New Private Property",null);
Console.WriteLine(pi_private.GetValue(mc,null));
Console.WriteLine();
// Methods
MethodInfo mi_protected = t.GetMethod("ProtectedMethod", bf);
MethodInfo mi_private = t.GetMethod("PrivateMethod", bf);
mi_protected.Invoke(mc,null);
mi_private.Invoke(mc,null);
Console.ReadLine();
輸出:
Protected Field
Private Field
New Private Field
Protected Property
Private Property
New Private Property
Protected Method Invoked
Private Method Invoked
事件,一樣可以操作, EventInfo :-)
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/nileel/archive/2007/04/17/1567587.aspx