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 );