I want to instantiate an object of a class using Activator.CreateInstance
. So, I have an ASP.Net MVC project under which i have a folder called Modal and under that i have one class TestClass.cs
.
Now i have another Class Library project. So have i written the instantiation login inside one of the class of my Class Library project and copy the .dll
file inside my main ASP.Net MVC project.
Now from my main project i requested to my recently add Class Library project to create an Object of the class by passing FullyQualified name of my class.
EX:
ASP.Net MVC Project
------------------------------
`
namespace MyProject.Modal
{
public class TestClass
{
public TestClass()
{
}
}
}
`
Class Library Project Code
`
namespace SpringNet.FactoryContext
{
public class CreateInstance
{
public Object GetClass(string FullyQualifiedName)
{
Object NewClassType = null;
Type ClassName = Type.GetType(FullyQualifiedName);
NewClassType = Activator.CreateInstance(ClassName);
return NewClassType;
}
}
}
`
Now i build my Class library project and added reference to my ASP.Net MVC Project and tries to create the object.
CreateInstance instance = new CreateInstance();
var NewObject = instance.GetClass("MyProject.Modal.TestClass");
After calling this i am getting exception
Type ClassName = Type.GetType(FullyQualifiedName);
at this line only i am not able to get the type of class what i am requesting for.
Please help. Thanks in advance.