Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following two classes:
C#
public class ApplicantInfo
    {
        public Int32 StudentId { get; set; }
        public String StudentName { get; set; }
        public DateTime DateOfBirth { get; set; } 
        public String Phone { get; set; }
        public String Email { get; set; }
        public Byte[] Image { get; set; }
        public SchoolDetail SchoolDetailObj { get; set; }      
    }   

public class SchoolDetail 
    {
        public string SchoolName { get; set; }
        public string SchoolPhone { get; set; }
        public string SchoolFax { get; set; }
        public string SchoolEmail { get; set; }      
    }    

In ApplicantInfo class i have placed the object of SchoolDetail class .

i want to get all property names of both these classes. So i write the following code
C#
ApplicantInfo appObj= new ApplicantInfo();

 Type ApplicantInfo = appObj.GetType();
                    PropertyInfo[] properties = ApplicantInfo.GetProperties();

foreach (PropertyInfo property in properties)
                          {
                             var propName= property.Name.ToString()
                             }

It gets the property name of ApplicantInfo perfectly but not the propery names of SchoolDetail class. It shows SchoolDetailObj as the property. But i want to get the property names of SchoolDetail class . Please help me .
Posted
Updated 8-May-15 1:38am
v4

Because you are using the object of ApplicantInfo class. Create a separate object for the SchoolDetail and iterate that property Collection in loop.
 
Share this answer
 
Comments
xpertzgurtej 2-Apr-15 6:00am    
I know that way..But my requirement is to get property names from the ApplicantInfo object without a separate loop..
SchoolDetailObj is a property of the ApplicantInfo class, which means you are retrieving the info correctly - but the system doesn't automatically return the properties of properties - otherwise it would return all the properties of the string, the byte array, the DateTime value, and so on.

If you want the properties of a property, you need to execute the same code again on that class as a separate loop.
 
Share this answer
 
Comments
xpertzgurtej 2-Apr-15 6:00am    
I know that way..But my requirement is to get property names from the ApplicantInfo object without a separate loop..I think there must be some way to handle this..
OriginalGriff 2-Apr-15 6:13am    
There isn't a way - there is no standard method which returns a heiracrhical structure of all properties.
You need a loop of some form, either direct (for, foreach...) or indirect via Linq
xpertzgurtej 2-Apr-15 8:34am    
Ok.thanks for your suggestions
OriginalGriff 2-Apr-15 8:46am    
You're welcome!
Try out below code :
C#
var properties = ApplicantInfo.GetProperties().ToDictionary(x => x, x => x.GetType().GetProperties());
foreach (var item in properties)
{
    Console.WriteLine(item.Key);
    foreach (PropertyInfo property in item.Value)
    {
         Console.WriteLine(" propName = " + property.Name.ToString());
    }
    Console.WriteLine();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900