user4455960
user4455960

Reputation: 49

get the value of the attributes in C#

this is a very simple question. such as this code:

        if(o == null)
        {
            o = new { };
        }
        PropertyInfo[] p1 = o.GetType().GetProperties();
        foreach(PropertyInfo pi in p1)
        {}

but like this:

ModelA.ModelB.ModelC.ModelD.ModelE

how to get ModelE's value by reflect ModelA

Upvotes: 1

Views: 160

Answers (3)

windfog
windfog

Reputation: 209

using nested function to do it like following:

    var testObj = new
{
    nameA = "A",
    ModelB = new
    {
        nameB = "B",
        ModelC = new
        {
            NameC = "C",
        }
    }
};
var result = ParseProperty(testObj, null, "ModelA");

public Dictionary<string, object> ParseProperty(object o, Dictionary<string, object> result, string preFix = null)
{
    result = result ?? new Dictionary<string, object>();

    if (o == null) return result;

    Type t = o.GetType();
    //primitive type or value type  or string or nested return
    if (t.IsPrimitive || t.IsValueType || t.FullName == "System.String" || t.IsNested) return result;

    var proerties = o.GetType().GetProperties();
    foreach (var property in proerties)
    {
        var value = property.GetValue(o);
        result.Add($"{preFix}.{property.Name}", value);
        //nested call
        ParseProperty(value, result, $"{preFix}.{property.Name}");
    }
    return result;
}

Upvotes: 1

roozbeh S
roozbeh S

Reputation: 1124

There is a solution explained here:

using a helper method:

public static class ReflectionHelper
{
    public static Object GetPropValue(this Object obj, String propName)
    {
        string[] nameParts = propName.Split('.');
        if (nameParts.Length == 1)
        {
            return obj.GetType().GetProperty(propName).GetValue(obj, null);
        }

        foreach (String part in nameParts)
        {
            if (obj == null) { return null; }

            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(part);
            if (info == null) { return null; }

            obj = info.GetValue(obj, null);
        }
        return obj;
    }
}

then the method can be used like this:

ModelA obj = new ModelA { */....*/ };
obj.GetPropValue("modelB.modelC.modelD.modelE");

please note that you should pass the property names to the function not the class names.

Upvotes: 3

Klaus G&#252;tter
Klaus G&#252;tter

Reputation: 12032

I assume these are all anonymous types because otherwise you could just do GetProperties() on the type of ModelE.

So you basically have to next 5 of your loops like

foreach (PropertyInfo pi1 in o1.GetType().GetProperties())
{
    if (pi.Name = "ModelB") // or some other criterion
    {
        o2 = pi1.GetValue(o1);
        foreach (PropertyInfo pi2 in o2.GetType().GetProperties())
        {
            if (pi.Name = "ModelC") // or some other criterion
            {
                o3 = pi1.GetValue(o2);
                // and so on
            }
        }
    }
}

Upvotes: 0

Related Questions