C#遍历类属性的方法

一、通过object.GetType() 对象获取属性集合

SHP_WR_RIVER moInfo = new SHP_WR_RIVER();
var props = moInfo.GetType().GetProperties();

二、通过 typeof 类名获取属性集合

Type shpWrRiver = typeof(SHP_WR_RIVER);
var  props = shpWrRiver.GetProperties();

对象遍历赋值

//原始对象
SHP_WR_RIVER moInfo = new SHP_WR_RIVER();
var props = moInfo.GetType().GetProperties();
//赋值新对象
SHP_WR_RIVER moInfoNew = new SHP_WR_RIVER();
foreach(var item in props)
{
   var setobj = moInfoNew .GetType().GetProperty(item.Name); 

   //从旧对象中获取值
   var value = item.GetValue(moInfo , null);

   setobj.SetValue(moInfoNew , value, null);
}

猜你喜欢

转载自blog.csdn.net/github_37830343/article/details/81181940
今日推荐