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

//获取数据
            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); }
}
}
}
}

 

posted @ 2021-06-01 17:17  博客YS  阅读(432)  评论(0编辑  收藏  举报