zoukankan      html  css  js  c++  java
  • 泛型 开放类型和构造类型(基础学习)

    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);

        }


  • 相关阅读:
    promiseall 使用一个ajax就可以调全部数据
    PHP中include和require的区别详解和使用建议
    phpredis中的connect和pconnect的区别
    <a>标签中的href="javascript:;"是什么意思?
    PHP中关于时间,时间戳 时区的设置问题
    javascript 超狠恶毒的禁用 右键 按键 禁用开发者工具 方法
    安装NoSQL数据库类型的redis 和 memcache数据库及其扩展
    XMind思维导图软件
    PHP代码中解决出现中文乱码的问题
    (七)mybatis-plus之generator(ftl模板生成:lombok swagger2 controloer的crud)
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/869814.html
Copyright © 2011-2022 走看看