C#反射

2021-04-19 11:30

阅读:671

标签:gpo   can   col   span   dbnull   模型   datatable   ble   nbsp   

public class ModelConvertHelperwhere T: new()
    {
        public static IList ConvertToModel(DataTable dt)
        {
            IList list = new List();

            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();

                //获得此模型的属性
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;

                    if (dt.Columns.Contains(tempName))
                    {
                        if (!pi.CanWrite)
                        {
                            continue;
                        }
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            pi.SetValue(t, value, null);
                        }
                    }
                }
                list.Add(t);
            }
            return list;
        }
    }

 

C#反射

标签:gpo   can   col   span   dbnull   模型   datatable   ble   nbsp   

原文地址:https://www.cnblogs.com/liaods/p/8665466.html


评论


亲,登录后才可以留言!