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.