2012-01-05 2 views
0

Employee의 값을 반환하고 emp 변수에 저장되는 GetEmployee() 메서드를 호출합니다.C에서 재귀 적으로 중첩 클래스에서 멤버 필드를 검색하는 방법

var emp = GetEmployee(); 

은 내가 emp 변수에서 반복적으로

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

처럼 모든 구성원의 나이, 이름과 같은 Employee 클래스의 필드와 중첩 된 클래스의 구성원을 검색해야합니다.

클래스 구조는 다음과 같습니다 :

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; 
} 

당신은 내가 이것을 달성 할 수있는 방법 좀 도와 줄래?

+0

당신이하고 싶은 일에 더 많은 맥락을 제공 할 수 있습니까? 자신의 라이브러리를 제 3 자 라이브러리로 옮기는 것을 여러 가지 방법으로 해결할 수 있습니다. 데이터로 수행하려는 작업은 사용자의 작업 수행 방식에 영향을 미칩니다. –

+0

Employee 객체를 만들거나 변수를 가져 오시겠습니까? 또는 런타임에 속성 이름을 가져 오시겠습니까? 나는 네가 무엇을 요구하는지 너무 확신하지 못한다. –

+0

그건 '재귀'라고하지 않습니다. – ThePower

답변

0

FieldInfo.GetValue(object)를 사용하는 것이 마지막으로 나는 내 질문에 대한 답을 가지고 :

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   
3

런타임에서 필드를 동적으로 검색하는 것에 대해 이야기하는 경우에는 여기에 utilitiy 이상의 일러스트레이션을위한 간단한 재귀 예제가 있습니다. 기본적으로 클래스 필드는 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); } 

출력 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 

, 당신은

관련 문제