亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定

 更新時(shí)間:2022年07月31日 16:13:28   作者:Darren Ji  
這篇文章介紹了C#使用反射實(shí)現(xiàn)延遲綁定的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

反射允許我們?cè)诰幾g期或運(yùn)行時(shí)獲取程序集的元數(shù)據(jù),通過(guò)反射可以做到:

● 創(chuàng)建類(lèi)型的實(shí)例
● 觸發(fā)方法
● 獲取屬性、字段信息
● 延遲綁定
......

如果在編譯期使用反射,可通過(guò)如下2種方式獲取程序集Type類(lèi)型:

  • 1、Type類(lèi)的靜態(tài)方法

Type type = Type.GetType("somenamespace.someclass");

  • 2、通過(guò)typeof

Type type = typeof(someclass);

如果在運(yùn)行時(shí)使用反射,通過(guò)運(yùn)行時(shí)的Assembly實(shí)例方法獲取Type類(lèi)型:

Type type = asm.GetType("somenamespace.someclass");

獲取反射信息

有這樣的一個(gè)類(lèi):

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public float Score { get; set; }

        public Student()
        {
            this.Id = -1;
            this.Name = string.Empty;
            this.Score = 0;
        }

        public Student(int id, string name, float score)
        {
            this.Id = id;
            this.Name = name;
            this.Score = score;
        }

        public string DisplayName(string name) 
        {
            return string.Format("學(xué)生姓名:{0}", name);
        }

        public void ShowScore()
        {
            Console.WriteLine("學(xué)生分?jǐn)?shù)是:" + this.Score);
        }
    }

通過(guò)如下獲取反射信息:

        static void Main(string[] args)
        {
            Type type = Type.GetType("ConsoleApplication1.Student");
            //Type type = typeof (Student);

            Console.WriteLine(type.FullName);
            Console.WriteLine(type.Namespace);
            Console.WriteLine(type.Name);

            //獲取屬性
            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                Console.WriteLine(prop.Name);
            }

            //獲取方法
            MethodInfo[] methods = type.GetMethods();
            foreach (MethodInfo method in methods)
            {
                Console.WriteLine(method.ReturnType.Name);
                Console.WriteLine(method.Name);
            }
            Console.ReadKey();
        }

 延遲綁定

在通常情況下,為對(duì)象實(shí)例賦值是發(fā)生在編譯期,如下:

Student stu = new Student();
stu.Name = "somename";

而"延遲綁定",為對(duì)象實(shí)例賦值或調(diào)用其方法是發(fā)生在運(yùn)行時(shí),需要獲取在運(yùn)行時(shí)的程序集、Type類(lèi)型、方法、屬性等。

            //獲取運(yùn)行時(shí)的程序集
            Assembly asm = Assembly.GetExecutingAssembly();

            //獲取運(yùn)行時(shí)的Type類(lèi)型
            Type type = asm.GetType("ConsoleApplication1.Student");

            //獲取運(yùn)行時(shí)的對(duì)象實(shí)例
            object stu = Activator.CreateInstance(type);

            //獲取運(yùn)行時(shí)指定方法
            MethodInfo method = type.GetMethod("DisplayName");
            object[] parameters = new object[1];
            parameters[0] = "Darren";

            //觸發(fā)運(yùn)行時(shí)的方法
            string result = (string)method.Invoke(stu, parameters);
            Console.WriteLine(result);
            Console.ReadKey();

到此這篇關(guān)于C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論