vendredi 1 novembre 2019

How get inner property value by reflection

I want to use the reflection to get the value from a class object. In this case, Rating is my main class which contains a property of Contribution class.

Following is the structure of my class

public class Rating
{
    public string personal_client_id { get; set; }
    public string risk_benefit_id { get; set; }
    public string type { get; set; }
    public string risk_event { get; set; }
    public string value { get; set; }

    public Contribution contribution { get; set; }

}

public class Contribution
{
    public string from { get; set; }
    public string value { get; set; }
}

Now I want the value from the contribution property like below.

var Rating = RatingObject.Where(x => x.personal_client_id == pcid).FirstOrDefault();
if (Rating  != null)
{
    Type type = Rating.GetType();
    PropertyInfo propertyInfo = type.GetProperty("contribution");
    var aa = propertyInfo.GetValue(Rating, null);

    //aa has the Contribution property now but i don't know how can i get the property value from 
   this object
   //Remember i dont want to do this **((Contribution)(aa)).from**

}
else
{
    return "";
}

Please help!





Aucun commentaire:

Enregistrer un commentaire