类中类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Reflection;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 二重类嵌套取内层的属性变量名
 9 {
10     public class Pro1
11     {
12         string name="张三";
13         int no=101;
14         bool flag=false;
15     }
16     public class Pro2
17     {        
18         Pro1 pro0 = new Pro1();
19     }
20     public class Program
21     {
22         static void Main(string[] args)
23         {
24              Pro2 p2 = new Pro2();//两重嵌套时  
25       FieldInfo fi2 = p2.GetType().GetField("pro0", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
26             object p1 = fi2.GetValue(p2);
27             FieldInfo fi1 = p1.GetType().GetField("name", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
28             string strName = fi1.GetValue(p1).ToString();//内层的属性及其属性的值
29          }
30      }
31 }
View Code


多重类嵌套时

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Reflection;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 三重类嵌套取内层的属性变量名
 9 {
10     public class Pro1
11     {
12         string name="张三";
13         int no=101;
14         bool flag=false;
15     }
16     public class Pro2
17     {        
18         Pro1 pro0 = new Pro1();
19         Pro1 pro1 = new Pro1();
20         Pro1 pro2 = new Pro1();
21         Pro1 pro3 = new Pro1();
22         Pro1 pro4 = new Pro1();
23         Pro1 pro5 = new Pro1();
24         Pro1 pro6 = new Pro1();
25         Pro1 pro7 = new Pro1();
26         Pro1 pro8 = new Pro1();
27         Pro1 pro9 = new Pro1();
28         Pro1 pro10 = new Pro1();
29         Pro1 pro11 = new Pro1();
30         Pro1 pro12 = new Pro1();
31         Pro1 pro13 = new Pro1();
32     }
33 
34     public class Pro3
35     {
36         Pro2 pro2 = new Pro2();
37     }
38         
39     public class Program
40     {
41         static void Main(string[] args)
42         {
43             
44             Pro3 p3 = new Pro3();//三重嵌套
45             List<string> ListStrP2 = new List<string>();
46             //为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。 
47 
48             //指定 BindingFlags.Public 可在搜索中包含公共字段。 
49 
50             //指定 BindingFlags.NonPublic 可在搜索中包含非公共字段(即私有字段和受保护的字段)。 
51 
52             //指定 BindingFlags.FlattenHierarchy 以便沿层次结构向上包括 public 和 protected 静态成员;不包括继承类中的 private 静态成员。 
53                      
54             //综合,通过中间层的遍历,循环取最内层的字段及其字段值
55             FieldInfo fi3 = p3.GetType().GetField("pro2", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);// 不关注父类中成员 :bindingflags.declaredonly 
56             object p2 = fi3.GetValue(p3);
57             FieldInfo[] arrayfi2 = p2.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
58             for (int i =0; i < arrayfi2.Length; i++)
59             {
60                 object p2i=p2.GetType().GetField(arrayfi2[i].Name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(p2);
61 
62                 ListStrP2.Add(p2i.GetType().GetField("name", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(p2i).ToString());
63                 Console.WriteLine(i+""+ListStrP2[i]);
64             }          
65         }
66     }
67 }
View Code