遍历实体类并给实体类赋值

//获取数据
            string sql =$@"
select 
t.*
from PushPlatformDatas t.id={caseid}
";
            using (OracleDataReader drs = OracleHelper.ExecuteReader(sql, System.Data.CommandType.Text))
            {
                if(drs.HasRows)
                {
                    while(drs.Read())
                    {
                        实体类 data = new 实体类(); 
System.Reflection.PropertyInfo[] properties
= data.GetType().GetProperties();

foreach (System.Reflection.PropertyInfo property in properties)
{
string comName = property.Name;
if (drs[comName] != null)
{
data.GetType().GetProperty(comName).SetValue(data, drs[comName].ToString(),
null);
}
else
{
data.GetType().GetProperty(comName).SetValue(data,
null, null); }
}
}
}
}
原文地址:https://www.cnblogs.com/yuanshuo/p/14838065.html