C# 遍历类所有属性

Unit unit = new Unit();
Type type = typeof(Unit);
//获取所有属性。
PropertyInfo[] properties = type.GetProperties();

// 遍历属性并打印。
foreach (PropertyInfo prop in properties)
{
//Console.WriteLine(prop.Name);
//Console.WriteLine(prop.GetValue(ab1));
//MessageBox.Show("属性名: " + prop.Name + "\r\n值: " + prop.GetValue(unit));

}

PS:类属性需要有get set 关键字才能获取

 

posted @ 2023-07-09 11:30  渔鸽  阅读(280)  评论(0)    收藏  举报