我试图为此找到解决方案,但我不能。鉴于以下课程和计划:
[Serializable]
public class SomeClass
{
public string OneProp { get; set; }
public string TwoProp { get; set; }
public string OneField;
public string TwoField;
}
[Serializable]
public class Class1
{
public static string gsFoo { get; set; }
public static string gsGar { get; set; }
public static string gsFooField;
public static string gsGarField;
public static SomeClass someClass = new SomeClass();
}
public class Program
{
private static void PrintValues(Type type)
{
foreach (var field in type.GetFields())
{
Console.WriteLine("{0}={1}", field.Name, field.GetValue(null));
var v = field.GetValue(null);
}
foreach (var prop in type.GetProperties())
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(type, null));
var v = prop.GetValue(null,null);
}
}
static void Main(string[] args)
{
Class1.gsGar = "gar prop";
Class1.gsFoo = "foo prop";
Class1.gsFooField = "foo field";
Class1.gsGarField = "gar field";
Class1.someClass.OneField = "1 field";
Class1.someClass.TwoField = "2 field";
Class1.someClass.OneProp = "1 prop";
Class1.someClass.TwoProp = "2 prop";
PrintValues(typeof(Class1));
while (true)
{
}
}
}
如果我设置Class1.SomeClass的属性和字段,我如何序列化Class1并查看SomeClass的名称/值,使其打印如下:
Class1.SomeClass.OneProp = "1 prop"
Class1.SomeClass.TwoProp = "2 prop"
Class1.SomeClass.OneField = "1 field"
Class1.SomeClass.TwoField = "2 field"
我可以使用GetType()和GetValue()从其他静态类型的FieldInfo和PropertyInfo获取非自定义类型的名称/值,但在这种情况下,FieldInfo(如果我没有记错)是元数据这个自定义类型,但我看不到如何获取其中的成员类型的名称和值。
我希望这是有道理的......