Get value of field by string

試著忘記壹切 提交于 2021-02-07 04:46:23

问题


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 Address from a object of TestClass1.


回答1:


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);



回答2:


try

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



回答3:


Your SubProperty member is actually a Field and 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 SubProperty field, and then the Address property.

This class will allow you to specify the return type of your property by closing the type T with the appropriate type. Then you will simply need to add to the first parameter the object whose 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.

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 Address property. Just make sure you first add a value to the said property.

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 SubMember class.

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.



来源:https://stackoverflow.com/questions/16760683/get-value-of-field-by-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!