c# 遍历类属集

SettingMessage sm = new SettingMessage();


Type t = sm.GetType();//获得该类的Type
//再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了

foreach (PropertyInfo pi in t.GetProperties())
{
object obj = pi.GetValue(sm, null);//用pi.GetValue获得值
//string name = pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作

//获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数
if (obj.GetType() == typeof(SettingItem))
{

}
}

原文地址:https://www.cnblogs.com/xsmhero/p/2860675.html