21

I don't know whether this is possible or not, but I would like to know if it is and, if so, how it works. So here is my question:

I have 2-3 custom model classes of my own. For example, Customer, Employee and Product. I also have a class name in a string. Based on the class name, I want to create an instance and return it to a view. How can I achieve this?

I know that one option is an if/else statement, but I would like a better, dynamic way.

1
  • What you are looking for is reflection, but as a general rule, if you are using reflection, you might be doing something wrong (there are cases where you need reflection, but it's not common).
    – Mike C.
    Commented Mar 16, 2013 at 13:29

4 Answers 4

42

Having the class name in string is not enough to be able to create its instance. As a matter of fact you will need full namespace including class name to create an object.

Assuming you have the following:

string className = "MyClass";
string namespaceName = "MyNamespace.MyInternalNamespace";

Than you you can create an instance of that class, the object of class MyNamespace.MyInternalNamespace.MyClass using either of the following techniques:

var myObj = Activator.CreateInstance(namespaceName, className);

or this:

var myObj = Activator.CreateInstance(Type.GetType(namespaceName + "." + className));

Hope this helps, please let me know if not.

4
  • I have class name in a string. so basically I dnt knw the class type. Then how could I create object of particular class. In "MyClass myObj = " I have to give MyClass. bt there can be any class.
    – Dhwani
    Commented Mar 18, 2013 at 5:25
  • I did exactly what you trying to do. As I have mentioned Besides the class name you need the namespace (as a string). Having them both, you can create instance of the object of that class the way I showed. I'll edit the answer to clarify. Thanks for aksing Commented Mar 18, 2013 at 12:33
  • 4
    Shouldn't that last code read: "var myObj = Activator.CreateInstance(Type.GetType(namespaceName + "." + className));" (missing dot?)
    – JCCyC
    Commented Mar 10, 2015 at 20:35
  • hey plz help me i have same problem but this solution doesn't work for me see Here please
    – SdSaati
    Commented Jul 26, 2016 at 18:21
9
    string frmName = "frmCustomer";
    //WorldCarUI. is the namespace of the form
    Type CAType = Type.GetType("WorldCarUI." + frmName );
    var myObj = Activator.CreateInstance(CAType);
    Form nextForm2 = (Form)myObj;
    nextForm2.Show();

this does works..

Regards Avi

2

the easiest way is to use Activator. Pass class name to GetType and Create new instance.

ClassInstance s1 = (ClassInstance)Activator.CreateInstance(Type.GetType("App.ClassInstance"));

public class ClassInstance
{
    public string StringData { get; set; }
}

Regards, Nik

0

Activator class does this job in .net and this technique is very usefull for dependency injection kind of scenarios.

string NameSpace = "ProjectName.YourNameSpace";
string ProbeClass = "CLassName";

ObjectHandle ProberHandle = Activator.CreateInstance(NameSpace, ProbeClass) as ObjectHandle;
ClassName Prober = ProberHandle.Unwrap() as ClassName;

Ensure that you unwrap before type casting otherwise it will give conversion error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.