public void Show()
    {
        //四种不同方法获取系统下mscorlib.dll 下的类型Type 
        int x = 10;
        Assembly a = Assembly.Load("mscorlib");
        Type t1 = typeof(int);
        Type t2 = x.GetType();
        Type t3 = a.GetType("System.Int32");
        Type t4 = Type.GetType("System.Int32");
        Response.Write(t1 == t2);
        Response.Write(t2 == t3);
        Response.Write(t3 == t4);
        //输出True True True 执行效率对比 t1>t2>t3>t4

        //通过循环找出mscorlib.dll里面的ParameterInfo
        Assembly b = Assembly.Load("mscorlib");
        foreach (Type t in b.GetTypes())
        {
            foreach (MethodInfo Method in t.GetMethods())
            {
                ParameterInfo[] Params = Method.GetParameters();
                foreach (ParameterInfo Param in Params)
                {
                    Response.Write(Param.Name);
                    Response.Write(Param.ParameterType);
                    Response.Write(Param.Position);
                    Response.Write(Param.IsOptional);
                }

            }

        }
    }

 //输出泛型的FullName,IsGenericParameter属性     //PrintTypeParams(typeof(List<string>));
    void PrintTypeParams(Type t)
    {
        Response.Write(t.FullName);
        foreach (Type ty in t.GetGenericArguments())
        {
            Console.WriteLine("{0}{1}{2}", ty.FullName, ty.IsGenericParameter, ty.IsGenericParameter ? ty.GenericParameterPosition : 0);
       
        }
   
    }


    void xiaobaigang()
    {
       
        Type listType1 = typeof(List<>);
        Response.Write(listType1.IsGenericType);//是泛型类型
        Response.Write(listType1.ContainsGenericParameters);//判断是否是开放泛型

        Type listType = typeof(List<int>);
        Response.Write(listType.IsGenericType);//是泛型类型
        Response.Write(listType.ContainsGenericParameters);//判断是否开放泛型

        //typeof(List<>).Equals(typeof(List<string>).GetGenericTypeDefinition() 是相等的
        if (typeof(List<>).Equals(typeof(List<string>).GetGenericTypeDefinition()))
        {
            Response.Write("GetGenericTypeDefinition属性测试");
        }
        //泛型  开放类型--->封闭类型动态实例化(构造类型)
        Type listType2 = typeof(List<>);
        Type listOfIntType = listType2.MakeGenericType(typeof(string));
        Response.Write(listOfIntType.FullName);

    }


posted on 2007-08-25 23:55  萧佰刚  阅读(384)  评论(0编辑  收藏  举报