国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.Net Framework 2.0范型的反射使用

2019-11-17 04:57:35
字體:
來源:轉載
供稿:網友

  在.Net Framework 2.0中引入了范型(Generic)的概念,這可以說是一個重大的改進它的好處我在這里也不用多說,到網上可以找到非常多的說明。

  我在這里要和大家說的是怎么通過反射使用范型的技術。

  一:首先看看范型的FullName

List<string> list = new List<string>();

System.Console.WriteLine(list.GetType().FullName);
System.Console.WriteLine();
  這個語句得到的是:

System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]。
  好長呀,分析一下其中的格式會看出一下幾個東東。

System.Collections.Generic.List -> 說明該Type是什么類型的。
  1、-> 應該是范型的標志。

System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ->是string類型的FullName。

  那么在看看這個語句會出現什么?

Dictionary<string, int> dic = new Dictionary<string, int>();
System.Console.WriteLine(dic.GetType().FullName);
System.Console.WriteLine();
  結果是:

System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]。
  更長,分析一下:

System.Collections.Generic.Dictionary -> 說明該Type是什么類型的。
  2 -> 還是是范型的標志。

System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ->是string類型的FullName。

System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ->是int類型的FullName。
  從上面的例子可以看出范型的類型和1.1時增加了兩個部分,分別是范型的標識部分和范型的參數類型FullName部分。

  首先看一下標志部分 `1和`2,猜測`標識了該類型是范型、后面的數字部分是說明了該范型需要幾個范型參數。

  現在還是猜測,下面根據猜測來應用我們自己的反射試驗一下吧:

  二:范型反射的試驗

  看看下面的代碼:

string tlistStr = "System.Collections.Generic.List`1[System.String]";

Type tList = Type.GetType(tlistStr);

Object olist = System.Activator.CreateInstance(tList);

MethodInfo addMList = tList.GetMethod("Add");

addMList.Invoke(olist, new object[] { "zhx" });

Console.WriteLine(olist.ToString());

System.Console.WriteLine();

string tDicStr = "System.Collections.Generic.Dictionary`2[[System.String],[System.Int32]]";

Type tDic = Type.GetType(tDicStr);

Object oDic = Activator.CreateInstance(tDic);

MethodInfo addMDic = tDic.GetMethod("Add");

addMDic.Invoke(oDic, new object[] {"zhx",1 });

Console.WriteLine(oDic.ToString());

System.Console.WriteLine();
  測試通過。不過大家要注重了。范型中的基礎類型如:string,int不能使用簡寫的,假如把 System.Collections.Generic.List`1[System.String] 寫成 System.Collections.Generic.List`1[string]是不能夠得到正確類型的。

  三:自已定義的范型反射的使用

  首先自己定義一個范型類:

namespace RefTest
{
 public class BaseClass<T,V,O>
 {
  T t;
  V v;
  O o;
  public void SetValue(T pt,V pv,O po)
  {
   this.t = pt;
   this.v = pv;
   this.o = po;
  }

  public override string ToString()
  {
   return string.Format("T:{0} V:{1} O:{2}", t.ToString(), v.ToString(), o.ToString());
  }
 }
}

  使用反射創建類型和調用方法:


string tBaseClassStr = "RefTest.BaseClass`3[[System.String],[System.Int32],
[System.Collections.Generic.Dictionary`2[[System.String],[System.Int32]]]]";

Type tBaseClass = Type.GetType(tBaseClassStr);

Object oBaseClass = Activator.CreateInstance(tBaseClass);

MethodInfo addMBaseClass = tBaseClass.GetMethod("SetValue");

addMBaseClass.Invoke(oBaseClass, new object[] {"zhx",1,oDic });

Console.WriteLine(oBaseClass.ToString());

System.Console.WriteLine();

  測試成功。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新津县| 郑州市| 英超| 藁城市| 汉川市| 砚山县| 江津市| 望谟县| 牡丹江市| 冕宁县| 平泉县| 分宜县| 行唐县| 盐亭县| 华安县| 陆川县| 青阳县| 陇西县| 电白县| 池州市| 鹤壁市| 麟游县| 光泽县| 克东县| 特克斯县| 重庆市| 林甸县| 嘉祥县| 广东省| 韶关市| 宜州市| 定安县| 宁乡县| 马鞍山市| 抚顺市| 丰宁| 博乐市| 磐石市| 桂平市| 聊城市| 雷山县|