我有以下程序循环遍历我的变量的所有属性:
class Program
{
static void Main(string[] args)
{
var person = new Person {Age = 30, Name = "Tony Montana", Mf = new Gender {Male = true,Female = false}};
var type = typeof(Person);
var properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("{0} = {1} : Value= {2}", property.Name, property.PropertyType, property.GetValue(person, null));
}
Console.Read();
}
}
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
public Gender Mf;
}
public class Gender
{
public bool Male;
public bool Female;
}
当我运行它时,我得到以下输出:
"Age = System.Int32 : Value= 30"
"Name = System.String : Value= Tony Montana"
我没有看到我的复杂类型 person.Mf。我如何遍历我的对象 person 并获取 person.Mf 的类型和 person.Mf 的属性(即 person.Mf.Male 等)?提前致谢