kilxy

kilxy

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  16 随笔 :: 0 文章 :: 88 评论 :: 43606 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
 

.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<stringint> dic = new Dictionary<stringint>();

 

            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,测试通过。不过大家要注意了。范型中的基础类型如:stringint不能使用简写的,如果把 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,测试成功。

 

希望对大家能够帮助。

posted on   kilxy  阅读(3007)  评论(5编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示