通過反射獲取匿名型別的屬性和屬性值
static void Main(string[] args)
{var Student1 = new { Name = "張三", Age = 11, Sex = "男", Address = "北京" };
var Student2 = new { Name = "李四", Age = 12, Sex = "男", Address = "西安" };
GetProperty(Student2);
Console.Read();
}
public static void GetProperty(params object[] customObject)
{
foreach (object obj in customObject)
{
foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())
{
Console.WriteLine(string.Format("PropertyName:{0},PropertyValue:{1}",
property.Name, property.GetValue(obj, null)));
}
}
}