• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

        .net的reflection (2)

        .net的reflection (2)

        更新時間:2022-05-14 文章作者:未知 信息來源:網絡 閱讀次數:

        一旦得到類對象,上表中所列的方法就能被叫來調用reflaction.第一個例子將檢查在CSharpReflectionSamples.Reflect類中的得到方法的信息。第一塊代碼用來定義類中的每個方法的名字,第二塊代碼將闡述得到方法信息。向下面所展示的,我們將用一個數組來保存用GetMethod()方法返回的方法信息。MethodInfo類包含信息為方法的名字,不管是否是虛擬的,它都是可見的,等等。

        namespace CSharpReflectionSamples
        {
        using System;
        using System.Reflection;

        /// <summary>
        /// Summary description for Client.
        /// </summary>
        public class Client
        {
        public static void Main()
        {
        // the typeof operator and the GetType method
        // both return a 'Type' object.
        Type type1 = typeof(Reflect);
        Reflect objTest = new Reflect(0);
        Type type2 = objTest.GetType();

        Console.WriteLine("Type of objTest is {0}", type2);
        Console.WriteLine();
        // pause
        Console.ReadLine();

        // reflect method information
        MethodInfo[] minfo = type1.GetMethods();
        // iterate through methods
        foreach (MethodInfo m in minfo)
        {
        Console.WriteLine(m);
        }
        Console.WriteLine();
        }
        }
        }

        下一個例子將展示動態得到對象有可能接觸的每個構造器的信息。類似與上面的例子,我們將返回一個包含每個構造器的信息ConstructorInfo對象。

        namespace CSharpReflectionSamples
        {
        using System;
        using System.Reflection;

        /// <summary>
        /// Summary description for Client.
        /// </summary>
        public class Client
        {
        public static void Main()
        {
        // the typeof operator and the GetType method
        // both return a 'Type' object.
        Type type1 = typeof(Reflect);
        Reflect objTest = new Reflect(0);
        Type type2 = objTest.GetType();

        Console.WriteLine("Type of objTest is {0}", type2);
        Console.WriteLine();
        // pause
        Console.ReadLine();

        // reflect constructors
        ConstructorInfo[] cinfo = type1.GetConstructors();
        // iterate through constructors
        foreach (ConstructorInfo c in cinfo)
        {
        Console.WriteLine(c);
        }
        }
        }
        }

        最后一部分,也許是reflection名字空間中最激動人心的部分,是在運行時動態調用類方法。有兩種方法,首先,我們將建立一個數組來存儲參數,這些參數被構造器用來建造對象。第二,一個System.Object對象將對抗CreateInstance方法的對象。以得到想得到對象的例子。最后,當我們有了對象的資料,我們能夠調用任何使用MethodParm數組的方法。下面是代碼:

        namespace CSharpReflectionSamples
        {
        using System;
        using System.Reflection;

        /// <summary>
        /// Summary description for Client.
        /// </summary>
        public class Client
        {
        public static void Main()
        {
        // the typeof operator and the GetType method
        // both return a 'Type' object.
        Type type1 = typeof(Reflect);
        Reflect objTest = new Reflect(0);
        Type type2 = objTest.GetType();



        // dynamic creation and invocation
        // instantiate the Reflect object, passing
        // a value of 1 to the constructor
        object[] oConstructParms = new object[] {1};
        object obj = Activator.CreateInstance(type1, oConstructParms);
        // invoke method of reflect object
        object[] oMethodParms = new object[] {17};
        int intResult = (int)type1.InvokeMember("AMethod", BindingFlags.Default |
        BindingFlags.InvokeMethod, null, obj, oMethodParms);
        Console.WriteLine("Result of calling AMethod on {0} is {1}",
        type1.Name, intResult);
        // pause
        Console.ReadLine();
        }
        }
        }


        這篇文章闡述了.net Reflaction的基礎,在下一部分,我將和大家討論進一步的話題,比如,動態發布中間語言,旗幟綁定,和中間語言原則。


        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 亚洲综合视频在线| 久久久久亚洲Av无码专| 亚洲人成网站在线播放2019| 免费观看的毛片大全| 亚洲图片激情小说| 免费阿v网站在线观看g| 亚洲国产美女视频| 免费福利网站在线观看| 亚洲欧美熟妇综合久久久久| 精品国产精品久久一区免费式| 亚洲一区二区三区在线观看网站| 免费观看的a级毛片的网站| 亚洲av永久无码| 亚洲成av人在片观看| 黄色网站软件app在线观看免费 | 97视频免费在线| 久久久久精品国产亚洲AV无码| 久久久久久国产a免费观看黄色大片 | 中国一级特黄高清免费的大片中国一级黄色片 | 国产大片91精品免费观看男同| 美国免费高清一级毛片| 亚洲伊人久久综合中文成人网 | 最近2019年免费中文字幕高清| 亚洲一级黄色大片| 四虎免费久久影院| 成人免费ā片在线观看| 亚洲美女免费视频| 麻豆精品国产免费观看| 一区二区三区免费看| 久久久久亚洲AV片无码下载蜜桃| 免费看国产成年无码AV片| 深夜福利在线视频免费| 亚洲av无码不卡一区二区三区| 色窝窝免费一区二区三区 | 亚洲高清不卡视频| 暖暖日本免费在线视频 | 久久亚洲2019中文字幕| 精品免费人成视频app| 国产精品亚洲专区无码牛牛| 久久亚洲精品无码播放| 麻豆国产精品免费视频|