Charan Raju C R
Charan Raju C R

Reputation: 768

How to retrieve member fields from nested classes recursively in C#

I am calling a method GetEmployee() which returns the value of type Employee and stored in emp variable.

var emp = GetEmployee();

I need to retrieve all the member fields of the Employee class like age, name and also members of nested classes like

[Address->DoorNum,Street,Zip]
[Phone->mobile,homePhone],
[Dependents->name,age,
[phone->mobile,homePhone]] 

recursively from the emp variable.

The class structure is as below:

class Employee
{
 int age;
 string name
 Address address;
 Phone[] phones;
 Dependents[] dependents;
}

class Address
{
 int DoorNum;
 string Street;
 int Zip;
}

class Phone
{
 string mobile;
 string homePhone;
}

class Dependents
{
 string name;
 int age;
 Phone depPhone;
}

Can you help me how can I achieve this?

Upvotes: 0

Views: 1444

Answers (2)

Charan Raju C R
Charan Raju C R

Reputation: 768

Finally I got answer for my question:

public Employee getEmployee() 
{
    var emp=EmployeeProxy.GetEmployee();         
    return new Employee {        
    name=emp.Name,         
        age=emp.Age,         
        address=emp.AddressType.Select(a=>new Address{        
             DoorNum=a.doorNum,        
             Street=ae.street,        
             Zip=a.zip,        
        }),        
        phones=new Phone[]{        
             mobile=PhoneType.Mobile,        
             homePhone=PhoneType.HomePhone,        
        },        
        dependents=emp.DependentsType.Select(d=>new Dependents{        
             name=d.Name,        
             age=d.Age,        
             depPhone=new Phone{        
                     mobile=d.DependentPhone.Mobile,        
                     homePhone=d.DependentPhone.HomePhone,        
             },        
        }),         
    }; //End of Return        
} //End of method        

Upvotes: 0

Joshua Honig
Joshua Honig

Reputation: 13215

In case you are talking about dynamically retrieving fields at runtime, here is a simple recursive example intended for illustration more than utilitiy. Note that by default class fields are inferred to be private.

public static void listFields(Type type, bool sameNamespace, int nestLevel = 1) {
    BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
    Console.WriteLine("\r\n{0}Fields of {1}:", tabs(nestLevel - 1), type.Name);
    foreach (FieldInfo f in type.GetFields(bf)) {
        Console.WriteLine("{0}{1} {2} {3}", tabs(nestLevel), (f.IsPublic ? "public" : "private"), f.FieldType.Name, f.Name);
        Type fieldType = (f.FieldType.IsArray) ? f.FieldType.GetElementType() : f.FieldType;
        if ((type != fieldType) && (!sameNamespace || fieldType.Namespace == type.Namespace)) {
            listFields(fieldType, sameNamespace, nestLevel + 2);
        }
    }
    Console.WriteLine();
}

private static String tabs(int count) { return new String(' ', count * 3); }

Output of listFields(typeof(Employee), true);:

Fields of Employee:
   private Int32 age
   private String name
   private Address address

      Fields of Address:
         private Int32 DoorNum
         private String Street
         private Int32 Zip

   private Phone[] phones

      Fields of Phone:
         private String mobile
         private String homePhone

   private Dependents[] dependents

      Fields of Dependents:
         private String name
         private Int32 age
         private Phone depPhone

            Fields of Phone:
               private String mobile
               private String homePhone

If you wanted to actually get the value of a field of an instance, you would use FieldInfo.GetValue(object)

Upvotes: 3

Related Questions