예제 #1
0
        /// <summary>
        /// convert to object list
        /// </summary>
        public static List <T> GetList <T>(MDataTable table, Data.DirverType attrDirverType = Data.DirverType.UnKnown) where T : new()
        {
            List <T> result;

            if (table == null)
            {
                result = new List <T>();
            }
            else
            {
                List <T>       list           = new List <T>();
                Type           typeFromHandle = typeof(T);
                var            attrDic        = AttributeHelper.GetProp2ColumnNameDics(attrDirverType, typeFromHandle);
                PropertyInfo[] properties     = typeFromHandle.GetProperties();
                foreach (var dataRow in table.Rows)
                {
                    T t = (T)((object)Activator.CreateInstance(typeFromHandle));
                    PropertyInfo[] array = properties;
                    for (int i = 0; i < array.Length; i++)
                    {
                        PropertyInfo propertyInfo = array[i];
                        var          columnName   = propertyInfo.Name;
                        if (attrDic.ContainsKey(columnName))
                        {
                            columnName = attrDic[columnName];
                        }
                        if (table.ContainsColumn(columnName))
                        {
                            if (propertyInfo.CanWrite)
                            {
                                var cellValue = dataRow[columnName].Value;
                                if (cellValue is DBNull || cellValue == null)
                                {
                                    propertyInfo.SetValue(t, null, null);
                                }
                                else
                                {
                                    var obj = TypeConverter.To(propertyInfo.PropertyType, cellValue);
                                    propertyInfo.SetValue(t, obj, null);
                                }
                            }
                        }
                    }
                    list.Add(t);
                }
                result = list;
            }
            return(result ?? new List <T>());
        }