Classes in class
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Reflection;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceproperty variable name of double class nested inner layer9 {Ten Public classPro1 One { A stringName="Zhang San"; - intno=101; - BOOLflag=false; the } - Public classPro2 - { -Pro1 Pro0 =NewPro1 (); + } - Public class Program + { A Static voidMain (string[] args) at { -Pro2 P2 =NewPro2 ();//Two-fold nesting time -FieldInfo Fi2 = p2. GetType (). GetField ("Pro0", BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public); - ObjectP1 =Fi2. GetValue (p2); -FieldInfo fi1 = p1. GetType (). GetField ("name", BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public); - stringStrName = Fi1. GetValue (p1). ToString ();//the properties of the inner layer and the values of their properties in } - } to}View Code
When multiple classes are nested
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Reflection;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 namespaceTriple-Nested property variable name for inner layer9 {Ten Public classPro1 One { A stringName="Zhang San"; - intno=101; - BOOLflag=false; the } - Public classPro2 - { -Pro1 Pro0 =NewPro1 (); +Pro1 Pro1 =NewPro1 (); -Pro1 Pro2 =NewPro1 (); +Pro1 Pro3 =NewPro1 (); APro1 Pro4 =NewPro1 (); atPro1 Pro5 =NewPro1 (); -Pro1 Pro6 =NewPro1 (); -Pro1 Pro7 =NewPro1 (); -Pro1 Pro8 =NewPro1 (); -Pro1 Pro9 =NewPro1 (); -Pro1 Pro10 =NewPro1 (); inPro1 pro11 =NewPro1 (); -Pro1 Pro12 =NewPro1 (); toPro1 pro13 =NewPro1 (); + } - the Public classPro3 * { $Pro2 Pro2 =NewPro2 ();Panax Notoginseng } - the Public class Program + { A Static voidMain (string[] args) the { + -Pro3 P3 =NewPro3 ();//Triple Nesting $list<string> ListStrP2 =Newlist<string>(); $ //in order to get the return value, you must specify BindingFlags.Instance or BindingFlags.Static. - - //Specifies that bindingflags.public can include public fields in the search. the - //Specifies that bindingflags.nonpublic can include non-public fields (that is, private fields and protected fields) in the search. Wuyi the //specifies bindingflags.flattenhierarchy to include both public and protected static members along the hierarchy, excluding private static members in the inheriting class. - Wu //synthesis, iterating through the middle layer, loops through the most inner fields and their field values -FieldInfo Fi3 = p3. GetType (). GetField ("Pro2", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);//do not care about members in the parent class: BindingFlags.DeclaredOnly About ObjectP2 =Fi3. GetValue (p3); $fieldinfo[] Arrayfi2 = p2. GetType (). GetFields (BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public); - for(inti =0; I < Arrayfi2. Length; i++) - { - ObjectP2i=p2. GetType (). GetField (Arrayfi2[i]. Name, BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public). GetValue (p2); A +Liststrp2.add (P2i. GetType (). GetField ("name", BindingFlags.NonPublic | BindingFlags.Instance |bindingflags.public). GetValue (P2i). ToString ()); theConsole.WriteLine (i+","+liststrp2[i]); - } $ } the } the}View Code
The properties, variable names, and their values of classes in a reflection class