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

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

我們看到,有一個(gè)名為Set的方法,這個(gè)會(huì)不會(huì)就是呢?
那我們來調(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ù)組的。
接下來,我們就會(huì)想,如何為數(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)用成功,其實(shí)還是比較簡單的。
可以看到,GetValue與SetValue有多個(gè)重載版本,如果想要反射多維數(shù)組,就要用到不同的重載,有興趣的朋友可以自己試試。
相關(guān)文章
C#實(shí)現(xiàn)表格數(shù)據(jù)轉(zhuǎn)實(shí)體的示例代碼
在實(shí)際開發(fā)過程中,特別是接口對接之類的,對于這種需求是屢見不鮮,現(xiàn)在很多在線平臺(tái)也都提供了像json轉(zhuǎn)實(shí)體、sql轉(zhuǎn)實(shí)體等。本文將用C#實(shí)現(xiàn)這一功能,需要的可以參考一下2022-09-09
Unity封裝延時(shí)調(diào)用定時(shí)器
這篇文章主要為大家詳細(xì)介紹了Unity封裝延時(shí)調(diào)用定時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程(附源碼)
這篇文章主要介紹了C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

