Examples of Reflection in C#

Create instance from assembly that is in your project References

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

  1. // Create instance of class DateTime  
  2. DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime)); 
  1. // Create instance of DateTime, use constructor with parameters (year, month, day)  
  2. 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.

  1. namespace Test  
  2. {  
  3.     public class Calculator  
  4.     {  
  5.         public Calculator() { ... }  
  6.         private double _number;  
  7.         public double Number { get { ... } set { ... } }  
  8.         public void Clear() { ... }  
  9.         private void DoClear() { ... }  
  10.         public double Add(double number) { ... }  
  11.         public static double Pi { ... }  
  12.         public static double GetPi() { ... }  
  13.     }  

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).
  1. // Dynamically load assembly from file Test.dll  
  2. Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll"); 
  1. // Get type of class Calculator from just loaded assembly  
  2. Type calcType = testAssembly.GetType("Test.Calculator"); 
  1. // Create instance of class Calculator  
  2. object calcInstance = Activator.CreateInstance(calcType); 
  1. // Get info about property: public double Number  
  2. PropertyInfo numberPropertyInfo = calcType.GetProperty("Number"); 
  1. // Get value of property: public double Number  
  2. double value = (double)numberPropertyInfo.GetValue(calcInstance, null); 
  1. // Set value of property: public double Number  
  2. numberPropertyInfo.SetValue(calcInstance, 10.0, null); 
  1. // Get info about static property: public static double Pi  
  2. PropertyInfo piPropertyInfo = calcType.GetProperty("Pi"); 
  1. // Get value of static property: public static double Pi  
  2. double piValue = (double)piPropertyInfo.GetValue(nullnull); 
  1. // Invoke public instance method: public void Clear()  
  2. calcType.InvokeMember("Clear",  
  3.     BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,  
  4.     null, calcInstance, null); 
  1. // Invoke private instance method: private void DoClear()  
  2. calcType.InvokeMember("DoClear", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, calcInstance, null); 
  1. // Invoke public instance method: public double Add(double number)  
  2. double value = (double)calcType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, new object[] { 20.0 }); 
  1. // Invoke public static method: public static double GetPi()  
  2. double piValue = (double)calcType.InvokeMember("GetPi",  
  3.     BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, nullnullnull); 
  1. // Get value of private field: private double _number  
  2. double value = (double)calcType.InvokeMember("_number",  
  3. BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
  4. null, calcInstance, null);