I access property value from a class object at run-time using reflection in C#.
public bool GetValue(string fieldName, out object fieldValue)
{
// Get type of current record
Type curentRecordType = _currentObject.GetType();
PropertyInfo property = curentRecordType.GetProperty(fieldName);
if (property != null)
{
fieldValue = property.GetValue(_currentObject, null).ToString();
return true;
}
else
{
fieldValue = null;
return false;
}
}
I pass Property Name as parameter: fieldName to this method.
Now, I need to access a property value from the child object of above class at run-time.
Can anyone there please guide how can I access child object property value?
Since you want to be able to find objects on arbitrarily-nested child objects, you need a function that you can call recursively. This is complicated by the fact that you may have children that refer back to their parent, so you need to keep track of which objects you've seen before in your search.
static bool GetValue(object currentObject, string propName, out object value)
{
// call helper function that keeps track of which objects we've seen before
return GetValue(currentObject, propName, out value, new HashSet<object>());
}
static bool GetValue(object currentObject, string propName, out object value,
HashSet<object> searchedObjects)
{
PropertyInfo propInfo = currentObject.GetType().GetProperty(propName);
if (propInfo != null)
{
value = propInfo.GetValue(currentObject, null);
return true;
}
// search child properties
foreach (PropertyInfo propInfo2 in currentObject.GetType().GetProperties())
{ // ignore indexed properties
if (propInfo2.GetIndexParameters().Length == 0)
{
object newObject = propInfo2.GetValue(currentObject, null);
if (newObject != null && searchedObjects.Add(newObject) &&
GetValue(newObject, propName, out value, searchedObjects))
return true;
}
}
// property not found here
value = null;
return false;
}
If you know what child object your property is in you can just pass the path to it, like so:
public bool GetValue(string pathName, out object fieldValue)
{
object currentObject = _currentObject;
string[] fieldNames = pathName.Split(".");
foreach (string fieldName in fieldNames)
{
// Get type of current record
Type curentRecordType = currentObject.GetType();
PropertyInfo property = curentRecordType.GetProperty(fieldName);
if (property != null)
{
currentObject = property.GetValue(currentObject, null).ToString();
}
else
{
fieldValue = null;
return false;
}
}
fieldValue = currentObject;
return true;
}
Instead of calling it like GetValue("foo", out val)
you would call it like GetValue("foo.bar", out val)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With