第15章 .NET中的反射

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            //TestClass.ShowMsg();            //TestClass.ShowMsg(new Message());            //Assembly asm = Assembly.GetExecutingAssembly();            //Object obj=asm.CreateInstance("ConsoleApplication2.Calculator",true);            //Calculator cal=(Calculator)obj;            //Object[] parameters = new Object[2] { 3, 5 };            //Object obj1 = asm.CreateInstance("ConsoleApplication2.Calculator", true, BindingFlags.Default, null, parameters, null, null);            //Calculator cal = (Calculator)obj1;            //Type t = typeof(Calculator);            //Calculator cal = new Calculator(3, 5);            //int result = (int)t.InvokeMember("Add", BindingFlags.InvokeMethod, null, cal, null);            //Console.WriteLine("result:" + result);            //Type t = typeof(Calculator);            //Object[] parameters = new Object[2] { 3, 5 };            //t.InvokeMember("Add", BindingFlags.InvokeMethod, null, t, parameters);            //Type t = typeof(Calculator);            //Calculator cal = new Calculator(3, 5);            //MethodInfo mi = t.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);            //mi.Invoke(cal, null);            //Type t = typeof(Calculator);            //Object[] parameters = new Object[2] { 6, 9 };            //MethodInfo mi = t.GetMethod("Add", BindingFlags.Public | BindingFlags.Static);            //mi.Invoke(null, parameters);            Console.ReadKey();        }    }    public class Message { }    public class TestClass    {        [Obsolete("請使用新方法")]        public static void ShowMsg()        {            Console.WriteLine("舊MSG");        }        public static void ShowMsg(Message msg)        {            Console.WriteLine("新MSG");        }    }    public class Calculator    {        private int x;        private int y;        public Calculator()        {            x = 0;            y = 0;            Console.WriteLine("Calculate()");        }        public Calculator(int x, int y)        {            this.x = x;            this.y = y;            Console.WriteLine("Calculate(int x, int y)");        }        public int Add()        {            int total = 0;            total = x + y;            Console.WriteLine("invoke instance method");            Console.WriteLine(string.Format("{0}+{1}={2}", x, y, total));            return total;        }        public static void Add(int x, int y)        {            int total = x + y;            Console.WriteLine("invoke static method");            Console.WriteLine(string.Format("{0}+{1}={2}", x, y, total));        }    }}
View Code

 

第15章 .NET中的反射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.