C# 通过字符串获取字段值

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

Get value of field by string

c#reflection

提问by Reignbeaux

I want to get the value of a field of an object by using a string as variable name. I tried to do this with reflection:

我想通过使用字符串作为变量名来获取对象字段的值。我试图用反射来做到这一点:

myobject.GetType().GetProperty("Propertyname").GetValue(myobject, null);

This works perfectly but now I want to get the value of "sub-properties":

这非常有效,但现在我想获得“子属性”的值:

public class TestClass1
{
    public string Name { get; set; }
    public TestClass2 SubProperty = new TestClass2();
}

public class TestClass2
{
    public string Address { get; set; }
}

Here I want to get the value Addressfrom a object of TestClass1.

在这里,我想AddressTestClass1.

回答by Dirk

You already did everything you need to do, you just have to do it twice:

你已经做了你需要做的一切,你只需要做两次:

TestClass1 myobject = ...;
// get SubProperty from TestClass1
TestClass2 subproperty = (TestClass2) myobject.GetType()
    .GetProperty("SubProperty")
    .GetValue(myobject, null);
// get Address from TestClass2
string address = (string) subproperty.GetType()
    .GetProperty("Address")
    .GetValue(subproperty, null);

回答by Yahia

try

尝试

 myobject.GetType().GetProperty("SubProperty").GetValue(myobject, null)
 .GetType().GetProperty("Address")
 .GetValue(myobject.GetType().GetProperty("SubProperty").GetValue(myobject, null), null);

回答by Mario Stopfer

Your SubPropertymember is actually a Fieldand not a Property, that is why you can not access it by using the GetProperty(string)method. In your current scenario, you should use the following class to first get the SubPropertyfield, and then the Addressproperty.

您的SubProperty成员实际上是 aField而不是 a Property,这就是您无法使用该GetProperty(string)方法访问它的原因。在您当前的场景中,您应该使用以下类首先获取SubProperty字段,然后获取Address属性。

This class will allow you to specify the return type of your property by closing the type Twith the appropriate type. Then you will simply need to add to the first parameter the objectwhose members you are extracting. The second parameter is the name of the field you are extracting while the third parameter is the name of the property whose value you are trying to get.

此类将允许您通过T使用适当的类型关闭类型来指定属性的返回类型。然后,您只需要将要提取其成员的对象添加到第一个参数。第二个参数是您要提取的字段的名称,而第三个参数是您尝试获取其值的属性的名称。

class SubMember<T>
{
    public T Value { get; set; }

    public SubMember(object source, string field, string property)
    {
        var fieldValue = source.GetType()
                               .GetField(field)
                               .GetValue(source);

        Value = (T)fieldValue.GetType()
                             .GetProperty(property)
                             .GetValue(fieldValue, null);
    }
}

In order to get the desired value in your context, simply execute the following lines of code.

为了在您的上下文中获得所需的值,只需执行以下代码行。

class Program
{
    static void Main()
    {
        var t1 = new TestClass1();

        Console.WriteLine(new SubMember<string>(t1, "SubProperty", "Address").Value);            
    }
}

This will give you the value contained in the Addressproperty. Just make sure you first add a value to the said property.

这将为您提供Address属性中包含的值。只要确保您首先为上述属性添加一个值。

But should you actually want to change the field of your class into a property, then you should make the following change to the original SubMemberclass.

但是,如果您真的想将类的字段更改为属性,则应该对原始SubMember类进行以下更改。

class SubMemberModified<T>
{
    public T Value { get; set; }

    public SubMemberModified(object source, string property1, string property2)
    {
        var propertyValue = source.GetType()
                                  .GetProperty(property1)
                                  .GetValue(source, null);

        Value = (T)propertyValue.GetType()
                                .GetProperty(property2)
                                .GetValue(propertyValue, null);
    }
}

This class will now allow you to extract the property from your initial class, and get the value from the second property, which is extracted from the first property.

该类现在允许您从初始类中提取属性,并从从第一个属性中提取的第二个属性中获取值。