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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15449800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:52:26  来源:igfitidea点击:

Create object instance of a class having its name in string variable

c#.netreflectionruntimesystem.reflection

提问by Dhwani

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 个自己的自定义模型类。例如,客户、员工和产品。现在我在一个字符串中有类名。并且基于字符串中的类名,我必须创建它的对象并返回到一个视图。我怎么能做到这一点?

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

我知道一种IF ELSE陈述方式,但我想尝试一种更好的“动态”方式......

采纳答案by Display Name

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.MyClassusing 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.

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

回答by Nik

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

问候, 尼克

回答by Avinash

    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

回答by amarnath chatterjee

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 unwrapbefore type casting otherwise it will give conversion error.

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