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 an instance from the assembly that is in your project References
The following examples create instances of DateTime
class from the System assembly.
Create an instance of the class DateTime
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));
Create instance of DateTime
, use constructor with parameters (year, month, day)
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),
new object[] { 2008, 7, 4 });
Create an instance from the dynamically loaded assembly
All the following examples try to access a sample class Calculator
from Test.dll
assembly. The calculator class can be defined like this.
namespace Test
{
public class Calculator
{
public Calculator() { ... }
private double _number;
public double Number { get { ... } set { ... } }
public void Clear() { ... }
private void DoClear() { ... }$ads=1
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 an instance of the Calculator class and to access its members (public/private, instance/static).
Dynamically load an assembly from the file Test.dll
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");
Get the type of class Calculator
from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");
Create an instance of the class Calculator
object calcInstance = Activator.CreateInstance(calcType);
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");
Get info about the property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);
Set value of the property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);
Get info about the static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");
Invoke public instance method: public void Clear()
calcType.InvokeMember("Clear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, null);
Invoke private instance method: private void DoClear()
calcType.InvokeMember("DoClear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);
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 });
invoke public static method: public static double GetPi()
double piValue = (double)calcType.InvokeMember("GetPi",
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, null);
Get the value of the private field: private double _number
double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);