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

        }


  • 相关阅读:
    httprunner运行报错问题:base url missed
    Locust性能模块浅谈
    如何对HTMLTestRunner 进行输出print 进行修改
    网易UI自动化测试工具Airtest中导入air文件中的方法
    如何在 idea 2019.3.4 中导入Github的项目并使用Git同步项目?
    Win10 Microsoft Store 微软商店 Error 0x00000193 解决方法
    读书笔记 |《计算机组成结构化方法》-01
    [MR] MapReduce 总结、回顾与吐槽
    [Git] 极简Git——关于Git的简要回顾
    [FlyWay] FlyWay工作原理
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/869814.html
Copyright © 2011-2022 走看看