alielie
2025-08-25 c4a3f8db90530a727ffab77c3bb5bb7d782792bf
HMI.Core/Util/BeanUtil.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Xml;
namespace HMI.Core.Util
{
@@ -20,5 +21,29 @@
            }
        }
        public static List<T> ReadBeanFromDataTable<T>(DataTable table)
        {
            if (table == null || table.Rows.Count <= 0) return new List<T>();
            List<T> list = new List<T>();
            foreach (DataRow row in table.Rows)
            {
                T bean = Activator.CreateInstance<T>();
                foreach (var prop in typeof(T).GetProperties())
                {
                    if (table.Columns.Contains(prop.Name))
                    {
                        var value = row[prop.Name];
                        if (value != DBNull.Value)
                        {
                            prop.SetValue(bean, Convert.ChangeType(value, prop.PropertyType));
                        }
                    }
                }
                list.Add(bean);
            }
            return list;
        }
    }
}