在.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();


Ok,测试通过。不过大家要注意了。范型中的基础类型如: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();


Ok,测试成功。
希望对大家能够帮助。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述