C# Reflection Demo

來源:互聯網
上載者:User
Reflection Examples [C#]

This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.

Create instance from assembly that is in your project References

The following examples create instances of DateTime class from the System assembly.

[C#]

// create instance of class DateTimeDateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

[C#]

// create instance of DateTime, use constructor with parameters (year, month, day)DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),                                                       new object[] { 2008, 7, 4 });
Create instance from dynamically loaded assembly

All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.

[C#]

namespace Test{    public class Calculator    {        public Calculator() { ... }        private double _number;        public double Number { get { ... } set { ... } }        public void Clear() { ... }        private void DoClear() { ... }        public double Add(double number) { ... }        public static double Pi { ... }        public static double GetPi() { ... }    }}

Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).

[C#]

// dynamically load assembly from file Test.dllAssembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

[C#]

// get type of class Calculator from just loaded assemblyType calcType = testAssembly.GetType("Test.Calculator");

[C#]

// create instance of class Calculatorobject calcInstance = Activator.CreateInstance(calcType);

[C#]

// get info about property: public double NumberPropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

[C#]

// get value of property: public double Numberdouble value = (double)numberPropertyInfo.GetValue(calcInstance, null);

[C#]

// set value of property: public double NumbernumberPropertyInfo.SetValue(calcInstance, 10.0, null);

[C#]

// get info about static property: public static double PiPropertyInfo piPropertyInfo = calcType.GetProperty("Pi");

[C#]

// get value of static property: public static double Pidouble piValue = (double)piPropertyInfo.GetValue(null, null);

[C#]

// invoke public instance method: public void Clear()calcType.InvokeMember("Clear",    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,    null, calcInstance, null);

[C#]

// invoke private instance method: private void DoClear()calcType.InvokeMember("DoClear",    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,    null, calcInstance, null);

[C#]

// invoke public instance method: public double Add(double number)double value = (double)calcType.InvokeMember("Add",    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,    null, calcInstance, new object[] { 20.0 });

[C#]

// invoke public static method: public static double GetPi()double piValue = (double)calcType.InvokeMember("GetPi",    BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,    null, null, null);

[C#]

// get value of private field: private double _numberdouble value = (double)calcType.InvokeMember("_number",    BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,    null, calcInstance, null);
相關文章

聯繫我們

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