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.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
Dhwani
  • 7,484
  • 17
  • 78
  • 139
  • 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. Mar 16 '13 at 13:29

4 Answers4

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.

Display Name
  • 4,672
  • 1
  • 33
  • 43
  • 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 Mar 18 '13 at 05: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 – Display Name Mar 18 '13 at 12:33
  • 4
    Shouldn't that last code read: "var myObj = Activator.CreateInstance(Type.GetType(namespaceName + "." + className));" (missing dot?) – JCCyC Mar 10 '15 at 20:35
  • hey plz help me i have same problem but this solution doesn't work for me see [Here please](http://stackoverflow.com/questions/38596936/i-have-a-helper-that-use-activator-createinstancesitaad-models-table-but-n) – SdSaati Jul 26 '16 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

Avinash
  • 140
  • 1
  • 2
  • 7
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

Nik
  • 21
  • 2
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.

amarnath chatterjee
  • 1,942
  • 16
  • 15