Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an object property value from a nested objected group using a string as the property name?

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.

like image 339
SamJolly Avatar asked Apr 16 '13 01:04

SamJolly


People also ask

How do I access nested objects?

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' }] };

How can I access and process nested objects arrays?

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.

What nested properties?

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.

How do I update nested objects?

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.

What are nested properties of an object in Java?

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.

How to check if a nested property exists on an 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.

How to access values of an object’s properties?

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.

How to get the property value based on the property name?

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); }


1 Answers

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 );
like image 144
von v. Avatar answered Oct 26 '22 15:10

von v.