samedi 19 janvier 2019

Get Property Value of Nested Classes Where Field or Property Names are not Known at Run Time

I am building a property search solution which would allow the user to lookup each property name and value in a list of nested properties where the names are not known in advance. The only class object known at run time is the parent class. In the below example I am passing the parent object (atype) to a method which returns (desired) a dictionary of all parent and child member keys and values.

Assumption: Each parent class has multiple nested classes. In the below the Parent is aType. It is child's play to retrieve the parent level properties with simple types, but not the nested class properties when the property names of the nested class properties are not known at run time.

The only solution I have found is a property lookup option where the full class path is known at run time. This is not an option when the class contains multiple classes of unknown properties. Some of the nested classes contain other class properties with their own set of fields and properties. Thus, no assumption may be made about class depth.

Desired: Provide an option for searching the parent and all nested classes without previous knowledge of the nested class(s) property names.

public static Dictionary<string, object> DictionaryFromType(object atype)
    {
        if (atype == null) return new Dictionary<string, object>();
        var t = atype.GetType();
        var props = t.GetProperties();
        var dict = new Dictionary<string, object>();
        foreach (var prp in props)
        {
            if (prp.PropertyType.IsClass)
            {
                 // The property Names of the Nested Class are not known at 
                 // this point. This is an example.
                 // At this point I only know the property is a class. 
                 // Passing the property class name yields no result.
                    var nestedValue = GetPropertyValue(atype, "childClass.nameField");
                    if (nestedValue != null)
                    dict.Add(prp.Name, nestedValue);
            }

            var value = GetPropertyValue(atype, prp.Name);
            if (value != null)
            dict.Add(prp.Name, value);
        }
        return dict;
    }

The below works perfectly well when the appropriate object and nesting are provided. It makes no attempt to lookup a nested object where only the object name is provided.

public static object GetPropertyValue(object obj, string propertyName)
    {
        var propertyNames = propertyName.Split('.');

        foreach (string t in propertyNames)
        {
            if (obj != null)
            {
                var propertyInfo = obj.GetType().GetProperty(t);
                if (propertyInfo != null)
                    obj = propertyInfo.GetValue(obj);
                else
                    obj = null;
            }
        }
        return obj;
    }





Aucun commentaire:

Enregistrer un commentaire