类中类

 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

相关文章: