日期:2014-05-18 浏览次数:20897 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Testa testa = new Testa(); testa.Name = "Eric"; testa.Value = "good"; Testb testb1 = new Testb(); testb1.Address = "上海"; testb1.Company = "微软"; Testb testb2 = new Testb(); testb1.Address = "北京"; testb1.Company = "IBM"; Testb[] testbArry = { testb1, testb2 }; testa.testbs = testbArry; FieldInfo[] fieldInfoes = testa.GetType().GetFields(); foreach (FieldInfo fieldInfo in fieldInfoes) { if (fieldInfo.FieldType == typeof(string)) { Console.WriteLine(string.Format("{0},{1}", fieldInfo.GetValue(testa), fieldInfo.FieldType)); } if (fieldInfo.FieldType == typeof(Testb[])) { //这里怎么写啊?取不出值,晕死了,搞了一晚上也没搞出来,好心人帮个忙吧。 } } Console.ReadLine(); } } public class Testa { public string Name; public string Value; public Testb[] testbs; } public class Testb { public string Company; public string Address; } }