110,534
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
else if (fieldInfo.FieldType.IsArray)
{
Console.WriteLine(string.Concat("发现数组,类型是:",fieldInfo.FieldType));
Console.WriteLine("数组的值分别是:");
Array list = (Array)fieldInfo.GetValue(testa);
foreach (var item in list)
{
Console.WriteLine("Item:");
FieldInfo[] itemInfos = item.GetType().GetFields();
foreach (var info in itemInfos)
{
Console.WriteLine(string.Format(" {0},{1}", info.GetValue(item), info.FieldType));
}
}
}