在字符串变量中创建名称的对象实例

时间:2022-05-15 04:45:18

I don't know the thing I am asking is available or not but I just want to know if it exists and how it works. So here is my question:

我不知道我问的问题是否可用,但我只是想知道它是否存在以及它是如何工作的。所以这是我的问题:

I have 2-3 custom model class of my own. For example, Customer, Employee and Product. Now I have class name in a string. and based on the class name coming in a string, I have to create its object and return to a VIEW. How could I achieve this?

我有自己的2-3个自定义模型类。例如,客户,员工和产品。现在我在一个字符串中有类名。并且基于字符串中的类名,我必须创建它的对象并返回到VIEW。我怎么能实现这个目标?

I know a option of IF ELSE statement but I want to try a better,"Dynamic" way...

我知道IF ELSE声明的一个选项,但我想尝试更好的“动态”方式......

4 个解决方案

#1


39  

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.

在string中使用类名不足以创建其实例。事实上,您将需要包含类名的完整命名空间来创建对象。

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:

您可以使用以下任一技术创建该类的实例,类MyNamespace.MyInternalNamespace.MyClass的对象:

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.

希望这有帮助,如果没有,请告诉我。

#2


8  

    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

关心Avi

#3


2  

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

最简单的方法是使用Activator。将类名传递给GetType并创建新实例。

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

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

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

Regards, Nik

此致,Nik

#4


0  

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

Activator类在.net中完成这项工作,这种技术对依赖注入类型的场景非常有用。

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.

确保在进行类型转换之前解包,否则会出现转换错误。

#1


39  

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.

在string中使用类名不足以创建其实例。事实上,您将需要包含类名的完整命名空间来创建对象。

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:

您可以使用以下任一技术创建该类的实例,类MyNamespace.MyInternalNamespace.MyClass的对象:

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.

希望这有帮助,如果没有,请告诉我。

#2


8  

    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

关心Avi

#3


2  

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

最简单的方法是使用Activator。将类名传递给GetType并创建新实例。

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

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

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

Regards, Nik

此致,Nik

#4


0  

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

Activator类在.net中完成这项工作,这种技术对依赖注入类型的场景非常有用。

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.

确保在进行类型转换之前解包,否则会出现转换错误。