深入反射生成數(shù)組的詳解
更新時間:2013年06月09日 09:58:38 作者:
本篇文章是對反射生成數(shù)組進行了詳細的分析介紹,需要的朋友參考下
今天在論壇有人問怎樣反射生成數(shù)組,突然又來了興致,決定試試
其實反射數(shù)組最難無非就是數(shù)組的初始化和數(shù)組的索引了,那么,如何初始化一個數(shù)組呢,數(shù)組是沒有構(gòu)造函數(shù)的,那么用InvokeMember(null, BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { })
是肯定不行的,用GetMethods來看看,這個類型都有哪些方法。
Type t = Type.GetType("System.Int32[]");
foreach(MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine(mi.Name);
}
結(jié)果如下:

我們看到,有一個名為Set的方法,這個會不會就是呢?
那我們來調(diào)用一下看看:
Type t = Type.GetType("System.Int32[]");
int[] array = new int[10];//初始化數(shù)組長度為10
array = (int[])(t.InvokeMember("Set", BindingFlags.CreateInstance,null, array, new object[] { 5 }));//這里將它的長度變?yōu)?看看是否能成功
Console.WriteLine(array.Length);
可以看到,輸出結(jié)果為5,這證明,的確是用Set方法來初始化數(shù)組的。
接下來,我們就會想,如何為數(shù)組的元素賦值及如何獲取它們的值呢?
再次看一下上面的方法列表,可以看到有GetValue與SetValue方法,那我們來試試:
Type t = Type.GetType("System.Int32[]");
object array = new object();
array = t.InvokeMember("Set", BindingFlags.CreateInstance, null, array, new object[] { 10 });
for (int i = 0; i < 10; i++)
t.GetMethod("SetValue", new Type[2] { typeof(object), typeof(int) }).Invoke(array, new object[] { i, i });
for (int i = 0; i < 10; i++)
Console.WriteLine(t.GetMethod("GetValue", new Type[] { typeof(int) }).Invoke(array, new object[] { i }));
結(jié)果如下:

調(diào)用成功,其實還是比較簡單的。
可以看到,GetValue與SetValue有多個重載版本,如果想要反射多維數(shù)組,就要用到不同的重載,有興趣的朋友可以自己試試。
其實反射數(shù)組最難無非就是數(shù)組的初始化和數(shù)組的索引了,那么,如何初始化一個數(shù)組呢,數(shù)組是沒有構(gòu)造函數(shù)的,那么用InvokeMember(null, BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { })
是肯定不行的,用GetMethods來看看,這個類型都有哪些方法。
復(fù)制代碼 代碼如下:
Type t = Type.GetType("System.Int32[]");
foreach(MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine(mi.Name);
}
結(jié)果如下:

我們看到,有一個名為Set的方法,這個會不會就是呢?
那我們來調(diào)用一下看看:
復(fù)制代碼 代碼如下:
Type t = Type.GetType("System.Int32[]");
int[] array = new int[10];//初始化數(shù)組長度為10
array = (int[])(t.InvokeMember("Set", BindingFlags.CreateInstance,null, array, new object[] { 5 }));//這里將它的長度變?yōu)?看看是否能成功
Console.WriteLine(array.Length);
可以看到,輸出結(jié)果為5,這證明,的確是用Set方法來初始化數(shù)組的。
接下來,我們就會想,如何為數(shù)組的元素賦值及如何獲取它們的值呢?
再次看一下上面的方法列表,可以看到有GetValue與SetValue方法,那我們來試試:
復(fù)制代碼 代碼如下:
Type t = Type.GetType("System.Int32[]");
object array = new object();
array = t.InvokeMember("Set", BindingFlags.CreateInstance, null, array, new object[] { 10 });
for (int i = 0; i < 10; i++)
t.GetMethod("SetValue", new Type[2] { typeof(object), typeof(int) }).Invoke(array, new object[] { i, i });
for (int i = 0; i < 10; i++)
Console.WriteLine(t.GetMethod("GetValue", new Type[] { typeof(int) }).Invoke(array, new object[] { i }));
結(jié)果如下:

調(diào)用成功,其實還是比較簡單的。
可以看到,GetValue與SetValue有多個重載版本,如果想要反射多維數(shù)組,就要用到不同的重載,有興趣的朋友可以自己試試。
相關(guān)文章
在WPF中合并兩個ObservableCollection集合
這篇文章介紹了在WPF中合并兩個ObservableCollection集合的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
利用windows性能計數(shù)器進行服務(wù)器性能監(jiān)控示例分享
這篇文章主要介紹了利用windows性能計數(shù)器進行服務(wù)器性能監(jiān)控的方法,大家可以參考擴展其它功能2014-01-01

