3

I have searched and tested for the last few hours and I can't seem to get the results I'm after. I'm trying to get a value from a nested property.

I can get the Property names with no issues with the following.

public static IList<string> GetClassPropertyNames(object myObject)
 {   
  List<string> propertyList = new List<string>();
     if (myObject != null)
     {
         var a = myObject.GetType().GetProperty("inobjects").PropertyType.GetProperties();
         foreach (var prop in a)
         {
             propertyList.Add(prop.Name.ToString());
         }             
     }
     return propertyList;               
    }

However if I then use one the of the names as string to get the property I can't get the right object or type in GetValue(Object, null) to get me the value I need.

I am using the following.

public static string GetNestedProperty(object myObject, string PropertyName)
    {
        string s = null;
        var a = myObject.GetType().GetProperty("inobjects").PropertyType.GetProperties();
        foreach (PropertyInfo pi in a)
        {
            if(pi.Name == PropertyName)
            {
                s = pi.GetValue(???, null).ToString();                   
            }                
        }
        return s;
    }

I want to keep the actual type generic as I am using "inobjects" as a property to get alot of properties for different classes and want a single way to access the Property names and values.

I'm just not able to get the right object at the right level and as a result keep getting issues with my type not being relevant. Any help would be appreciated.

1
  • I'm a little confused about what you mean by nested property here. Can you give an example set of classes? Commented Jul 26, 2014 at 2:51

3 Answers 3

4

Your problem really boils down to the fact that you need to obtain an instance of the immediate property from which to extract the value of the nested property.

In concrete words - you need to obtain the value (instance) of the immediate property "inobjects" before you can resolve the value of the nested property propertyName.

Ideally you will cache the PropertyInfo instance but to make the code work in the simplest way possible - resolve the value like this:

...
if (pi.Name == PropertyName)
{
    var value = 
        myObject.GetType()
                .GetProperty("inobjects")
                .GetValue(myObject, null);

    s = (string) pi.GetValue(value, null);
}

You might be interested to know that you do not need to manually iterate over each property and that you could use GetProperty instead (which will return null if the property does not exist). I took the liberty of refactoring your code for you:

public static string GetNestedProperty(object value, string propertyName)
{
    var type = value.GetType();
    var property = type.GetProperty("inobjects");
    value = property.GetValue(value, null);
    type = property.PropertyType;

    property = type.GetProperty(propertyName);

    if (property == null)
    {
        return null;
    }

    value = property.GetValue(value, null);
    return (string) value;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's great, I had to change to a direct string Output due to some of the properties stored using the .ToString() but that was it, getting the object as a value was the part I missed.!!!
0

A couple of things that I can see:

1) (this may not be a problem, but won't hurt to post it) you are using a foreach loop without any sort of break when the condition is met which can cause some trouble because you might have another fulfillment to the condition.

2) this is regarding your ??? that is actually for the object that you want to get the value from. So, if you know how far in the object the nested object is, just put in a condition to extract that object and hold it until that line. If it's the "inobjects" object, just break the setting of a up so that you can get it. Something like this:

var rawNestedObject = myObject.GetType().GetProperty("inobjects");
var a = nestedObject.PropertyType.GetProperties();

then your ??? marks can get replaced with nestedObject which would be reference that gets extracted from rawNestedObject.

APPEND: Also, you are not using any flags on your GetProperty calls which default to public instance properties. I don't know what your standard is for naming public members, so feel free to disregard this paragraph if you are accessing a public instance property.

Comments

0

This Will get you the value of the inner property:

var a = obj.GetType().GetProperty("inobjects").PropertyType.GetProperties();
foreach (var item in a)
{
     if (item.Name == PropertyName)
     {
         var test = obj.GetType().GetProperty("inobjects").GetValue(obj,null);
         s = (string)test.GetType().GetProperty(PropertyName).GetValue(test, null);
     }
}

Its not the cleanest way but this example will do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.