C#遍历类的所有属性

示例

Type type = obj.GetType();
//限制返回属性。如果需要所有属性,请不要提供标志。
BindingFlags flags =BindingFlags.Public| BindingFlags.Instance; 
PropertyInfo[] properties = type.GetProperties(flags);

foreach (PropertyInfo property in properties)
{
    Console.WriteLine("Name: " +property.Name+ ", Value: " + property.GetValue(obj, null));
}