GSharper

Tuesday, 13 March 2007

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

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: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home