C # reflection demo

Source: Internet
Author: 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 classDatetimeDatetimeDatetime = (Datetime)Activator. Createinstance (Typeof(Datetime));

[C #]

// Create instanceDatetime,Use constructor with Parameters(Year, month, day)DatetimeDatetime = (Datetime)Activator. Createinstance (Typeof(Datetime),New Object[] {2008, 7, 4 });
Create instance from Dynamically Loaded assembly

All the following examples try to accessSample class CalculatorFrom 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 ReflectionTo 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 FileTest. dllAssemblyTestassembly =Assembly. LoadFile (@ "C: \ test. dll");

[C #]

 
// Get type of classCalculatorFrom just loaded assemblyTypeCalctype = testassembly. GetType ("Test. Calculator");

[C #]

// Create instance of classCalculatorObjectCalcinstance =Activator. Createinstance (calctype );

[C #]

 
// Get info about property:Public double numberPropertyinfoNumberpropertyinfo = calctype. getproperty ("Number");

[C #]

 
// Get value of property:Public double numberDoubleValue = (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 piPropertyinfoPipropertyinfo = calctype. getproperty ("Pi");

[C #]

 
// Get value of static property:Public static double piDoublePivalue = (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)DoubleValue = (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 ()DoublePivalue = (Double) Calctype. invokemember ("Getpi",Bindingflags. Invokemethod |Bindingflags. Static |Bindingflags. Public,Null,Null,Null);

[C #]

   // get value of private field:  private double _ number   double  value = ( double ) calctype. invokemember ( "_ number" ,  bindingflags . getfield |  bindingflags . instance |  bindingflags . nonpublic,  null , calcinstance,  null ); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.