C#创建类的实例并按字符串中的名称设置属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11958825/
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-09 19:50:34  来源:igfitidea点击:

C# creating instance of class and set properties by name in string

c#reflection

提问by Adrian Ksi??arczyk

I have some problem. I want to creating instance of class by name. I found Activator.CreateInstancehttp://msdn.microsoft.com/en-us/library/d133hta4.aspxand it works fine, and I found this: Setting a property by reflection with a string valuetoo.

我有一些问题。我想按名称创建类的实例。我找到了Activator.CreateInstancehttp://msdn.microsoft.com/en-us/library/d133hta4.aspx并且它工作正常,我发现了这一点: 通过反射设置一个字符串值的属性

But how to do both od this? I mean, I know the name of class, I know all properties in that class and I have this in string. For example:

但是如何做到这一点呢?我的意思是,我知道类的名称,我知道该类中的所有属性,并且我将其放在字符串中。例如:

string name = "MyClass";
string property = "PropertyInMyClass";

How to create instance and set some value to properties ?

如何创建实例并为属性设置一些值?

采纳答案by Darin Dimitrov

You could use Reflection:

你可以使用反射:

using System;
using System.Reflection;

public class Foo
{
    public string Bar { get; set; }
}

public class Program
{
    static void Main()
    {
        string name = "Foo";
        string property = "Bar";
        string value = "Baz";

        // Get the type contained in the name string
        Type type = Type.GetType(name, true);

        // create an instance of that type
        object instance = Activator.CreateInstance(type);

        // Get a property on the type that is stored in the 
        // property string
        PropertyInfo prop = type.GetProperty(property);

        // Set the value of the given property on the given instance
        prop.SetValue(instance, value, null);

        // at this stage instance.Bar will equal to the value
        Console.WriteLine(((Foo)instance).Bar);
    }
}

回答by Tomá? Václavík

If you had System.TypeLoad Exception, your class name is wrong.

如果您有 System.TypeLoad 异常,则您的类名是错误的。

To method Type.GetType you must enter assembly-qualified name. That is with the project name For example: GenerateClassDynamically_ConsoleApp1.Foo

要使用 Type.GetType 方法,您必须输入程序集限定名称。即与项目名称例如:GenerateClassDynamically_ConsoleApp1.Foo

If it is in another assembly jou must enter assembly name after comma (details on https://stackoverflow.com/a/3512351/1540350): Type.GetType("GenerateClassDynamically_ConsoleApp1.Foo,GenerateClassDynamically_ConsoleApp1");

如果它在另一个程序集中,则必须在逗号后输入程序集名称(详情见https://stackoverflow.com/a/3512351/1540350): Type.GetType("GenerateClassDynamically_ConsoleApp1.Foo,GenerateClassDynamically_ConsoleApp1");

回答by Hanan

Type tp = Type.GetType(Namespace.class + "," + n.Attributes["ProductName"].Value + ",Version=" + n.Attributes["ProductVersion"].Value + ", Culture=neutral, PublicKeyToken=null");
if (tp != null)
{
    object o = Activator.CreateInstance(tp);
    Control x = (Control)o;
    panel1.Controls.Add(x);
}