kilxy

kilxy

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 

.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 2006-04-29 14:40  kilxy  阅读(2995)  评论(5编辑  收藏  举报