mardi 20 octobre 2015

Get value of a property in a component in a class dynamically

I have the following class

public class Statement
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public Narrative Narrative { get; set; }
}

public class Narrative
{
    public string Narrative1 { get; set; }
    public string Narrative2 { get; set; }
    public string Narrative3 { get; set; }
}

I want to get the Narrative1 property dynamically. Can anyone help how to achieve this?

I know how to get the value of a property name of a in a class using reflection.And here is the code for the same.

public static object ReflectPropertyValue(object source, string property)
{
    return source.GetType().GetProperty(property).GetValue(source,null);
}

Can anyone help how to get the value of a property inside another property?

Thanks





Aucun commentaire:

Enregistrer un commentaire