jeudi 26 janvier 2023

How to find property and their values if they are property of another class using reflection in C#?

How To Get Properties and Their values ? //Employee Class

public class Employee
    {
        public string Name { get; set; }
        public Address HomeAddress { get; set; }
        public DateTime Joiningdate_With_Time{ get; set; }
        public List<Phone> PhoneNumbers { get; set; }
    }

//Address Class

public class Address
    {
        public string Road{ get; set; }
        public string PostOffice{ get; set; }
    }

//Phone Class

    public class Phone
    {
        public string Number { get; set; }
        public string Extension { get; set; }
    }
public static string GetNestedPropertiesValues(object obj){
//This should return a string which can contains property Name & Their values of Instructor Class 
// Here reflection should be used 
}
Employee e1 = new Employee{
Name = "Asish",
HomeAddress = new Address{Road = "8",PostOffice="Mumbai"},
Joiningdate_With_Time = new DateTime(2022,5,28,12,5,55),
PhoneNumbers = new List<Phone>{
new Phone {Number = "123456",Extension="+330"},
new Phone {Number = "22586",Extension="+980"},
 }
};

Console.WriteLine(GetNestedPropertiesValues(e1));//This will print the values with property Name

GetNestedPropertiesValues method should return all the values with property names. Here Only Use Reflection and Recursion can be used.





Aucun commentaire:

Enregistrer un commentaire