Creating instances using reflection
Let's say,
we have a Class library, ClassLib. This library contains a type CystomType which implements a certain Interface ICustomtype. This interface defines the contract which states a method CustomMethod.
Let's create a instance of CustomType at runtime using reflection and use the method
Don't forget to include the following lines
we have a Class library, ClassLib. This library contains a type CystomType which implements a certain Interface ICustomtype. This interface defines the contract which states a method CustomMethod.
Let's create a instance of CustomType at runtime using reflection and use the method
Assembly assembly = Assembly.LoadFrom("ClassLib.dll");
string assemblyName = assembly.FullName;
ObjectHandle wrappedCustomType =
Activator.CreateInstance(assemblyName,"ClassLib.CustomType");
ICustomType myCustomType = (ICustomType)wrappedCustomType.Unwrap();
myCustomType.CustomMethod();
Don't forget to include the following lines
using System.Reflection;
using System.Runtime.Remoting;
Labels: Reflection, Runtime instantiation
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home