Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of field by string

Tags:

c#

reflection

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.

like image 904
Reignbeaux Avatar asked May 26 '13 15:05

Reignbeaux


People also ask

How do you find the value of field through reflection?

If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.

What is C# FieldInfo?

FieldInfo objects are obtained by calling either the GetFields or GetField method of a Type object. Fields are variables defined in the class. FieldInfo provides access to the metadata for a field within a class and provides dynamic set and get functionality for the field.


3 Answers

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);
like image 65
Dirk Avatar answered Nov 16 '22 12:11

Dirk


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.

like image 39
Mario Stopfer Avatar answered Nov 16 '22 14:11

Mario Stopfer


try

 myobject.GetType().GetProperty("SubProperty").GetValue(myobject, null)
 .GetType().GetProperty("Address")
 .GetValue(myobject.GetType().GetProperty("SubProperty").GetValue(myobject, null), null);
like image 34
Yahia Avatar answered Nov 16 '22 12:11

Yahia