I have a nested set of objects ie some properties are custom objects. I would like to get a object property value within the hierarchy group using a string for the property name, and some form of "find" method to scan the hierarchy to find a property with matching name, and get its value.
Is this possible and if so how?
Many thanks.
EDIT
Class definition may be in pseudocode:
Class Car
Public Window myWindow()
Public Door myDoor()
Class Window
Public Shape()
Class Door
Public Material()
Car myCar = new Car()
myCar.myWindow.Shape ="Round"
myDoor.Material = "Metal"
All a little contrived, but could I "find" the value of the "Shape" property by using the magic string "Shape" in some form of find function, starting from the top object. ie:
string myResult = myCar.FindPropertyValue("Shape")
Hopefully myResult = "Round".
This is what I am after.
Thanks.
A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };
You can access a nested array of objects either using dot notation or bracket notation. JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of an object. Both arrays and objects expose a key -> value structure.
The value of many properties is a reference to another resource. If the value of a property is a reference to a resource, the PropertyRequest might contain a NestedPropertyName object instead of the PropertyName object for the property.
To update nested properties in a state object in React: Pass a function to setState to get access to the current state object. Use the spread syntax (...) to create a shallow copy of the object and the nested properties. Override the properties you need to update.
Nested properties of an object are properties within properties. An object can contain another object as a property. we can allocate another object as a property of an object. In other words, an object can have a property that is another object. Values of an object’s properties can be another object.
In the code snippet we use the optional chaining operator to check if a nested property exists on the object. Using the ?. operator is similar to accessing a property on an object with .(dot) notation, however ?. does not throw an error when a nested property does not exist.
Values of an object’s properties can be another object. We can access nested properties of an object or nested objects using the dot notation (.) or the bracket notation []. Nested properties of an object can be accessed by chaining key or properties names in the correct sequence order.
I have a method which gets the property value based on the property name as follows: public object GetPropertyValue (object obj ,string propertyName) { var objType = obj.GetType (); var prop = objType.GetProperty (propertyName); return prop.GetValue (obj, null); }
Based on classes you showed in your question, you would need a recursive call to iterate your object properties. How about something you can reuse:
object GetValueFromClassProperty(string propname, object instance)
{
var type = instance.GetType();
foreach (var property in type.GetProperties())
{
var value = property.GetValue(instance, null);
if (property.PropertyType.FullName != "System.String"
&& !property.PropertyType.IsPrimitive)
{
return GetValueFromClassProperty(propname, value);
}
else if (property.Name == propname)
{
return value;
}
}
// if you reach this point then the property does not exists
return null;
}
propname
is the property you are searching for. You can use is like this:
var val = GetValueFromClassProperty("Shape", myCar );
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