Following is the code to load any assembly and reading its classes to make dynamic instances out of them. You can further call the dynamic methods as well.
//Loading an assembly
Assembly objAssembly = Assembly.LoadFrom(Server.MapPath(@"bin\test.dll"));
//Getting list of all types in a the assembly
Type[] type = objAssembly.GetTypes();
// You can also define a typename if that is already known
//Type t = objAssembly.GetType("testnamespace.testclass");
//********************************************************
//Making Instance of object by passing a particular type
object newInstance = Activator.CreateInstance(type[0]);
//Calling the method of the newly created instance by using type name
string str = (string)type[0].GetMethod("testmethod").Invoke(newInstance, null);
Your test.dll will look something like this.....
namespace testnamespace
{
public class testclass
{
public static string testmethod()
{
return "hello";
}
}
}
No comments:
Post a Comment