Question

How can I get field's value if it's defined as automatic property?
I don't know why but first time I encounter such simple task with such an unexplained method called GetValue which don't work as I want to and usually throws all kinds of exceptions instead of doing its original simple job..

Some code for example:

Class A
{
   public int Age { get; set;}
}

Now assume that after reflection I hold A instances' fields in structure of FiledInfo[] .
Now I have found the relevant fieldInfo in the above array and his name is :
{Int32 k__BackingField} sound strange,anyway..
How do I use GetValue() in order to get the int value? As I said I tried many things..

EDIT: (this is partial simplified code - don't get mad)

private static string foo1<T>(T o)
        {
            var type = o.GetType();
            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
           ....
            foo(fields);
         }
}

private static void foo(FieldInfo[] fields)  
        {

            foreach (FieldInfo field in fields)
            {

                if (field.FieldType.IsValueType)
                {
                    var fieldNameLength = field.Name.IndexOf(">") - (field.Name.IndexOf("<")+1);
                    var fieldName = field.Name.Substring(field.Name.IndexOf("<")+1, fieldNameLength);
                    var fieldValue = field.ReflectedType.GetProperty(fieldName).GetValue(field.ReflectedType, null)
                }
             }
}
Was it helpful?

Solution 2

Instead of var fieldValue = field.ReflectedType.GetProperty(fieldName).GetValue(field.ReflectedType, null) you should just have: var fieldValue = field.GetValue(o, null) Notice you need to pass your instance of o in. Really, you should do what L.B posted and find your property by name, or if you don't know the name, enumerate through through them via myType.GetProperties

Here's your code modified to work with properties:

private static void foo1<T>(T o)
{
    var type = o.GetType();
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

    foo(properties, o);
}

private static void foo(PropertyInfo[] properties, object o)
{
    foreach (PropertyInfo property in properties)
    {
        if (property.PropertyType.IsValueType)
        {
            var propertyValue = property.GetValue(o, null);
            //do something with the property value?
        }
    }
}

EDIT: You may want to ensure that the property has a getter (see: How do you find only properties that have both a getter and setter?), or maybe that it's an auto-property (see: How to find out if a property is an auto-implemented property with reflection?) but I'm guessing this is not necessarily a requirement for you, probably just the proper usage of the GetValue method or how to use reflection to inspect types.

EDIT: Here's the same code using fields if you still wish to use them:

private static void foo1<T>(T o)
{
    var type = o.GetType();
    var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

    foo(fields, o);
}

private static void foo(FieldInfo[] fields, object o)
{
    foreach (FieldInfo field in fields)
    {
        if (field.FieldType.IsValueType)
        {
            var fieldValue = field.GetValue(o);
            //do something with the field value?
        }
    }
}

OTHER TIPS

A a = new A() { Age = 33 };
var age = a.GetType().GetProperty("Age").GetValue(a);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top