泛型 開放類型和構造類型(基礎學習)

來源:互聯網
上載者:User

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

    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.