mercredi 18 avril 2018

Get the full name of a nested property and its parents

How can I get the full name of a nested property by its parent using reflection in C#? I mean, for example we have this classes:

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime? DateOfBirth { get; set; }
    public Grade Grade { get; set; }
}

public class Grade
{
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }
    public GradGroup GradGroup { get; set; }
}

public class GradGroup
{
    public int Id { get; set; }
    public string GroupName { get; set; }
}

I have "GradGroup" and Student as method parameters and want "Grade.GradeGroup" as output:

public string GetFullPropertyName(Type baseType, string propertyName)
    {
        // how to implement that??
    }

Call the method:

GetFullPropertyName(typeof(Student), "GradeGroup"); // Output is Grade.GradeGroup





Aucun commentaire:

Enregistrer un commentaire