예제 #1
0
        public static T FillObject <T>(Type objType, IDataReader dr) where T : class, new()
        {
            T result = default(T);

            try
            {
                List <PropertyInfo> properties = BusinessProperty.GetProperties(objType);
                int[] ordinals = BusinessProperty.GetOrdinals(properties, dr);
                if (dr.Read())
                {
                    result = BusinessProperty.CreateObject <T>(dr, properties, ordinals);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (!dr.IsClosed)
                {
                    dr.Close();
                }
            }
            return(result);
        }
예제 #2
0
        public static C FillCollection <T, C>(Type objType, IDataReader dr) where T : class, new() where C : ICollection <T>, new()
        {
            C result = (default(C) == null) ? Activator.CreateInstance <C>() : default(C);

            try
            {
                List <PropertyInfo> properties = BusinessProperty.GetProperties(objType);
                int[] ordinals = BusinessProperty.GetOrdinals(properties, dr);
                while (dr.Read())
                {
                    T item = BusinessProperty.CreateObject <T>(dr, properties, ordinals);
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (!dr.IsClosed)
                {
                    dr.Close();
                }
            }
            return(result);
        }
예제 #3
0
        public static DataTable ConvertToDataTable <T>(IList <T> objects)
        {
            List <PropertyInfo> properties = BusinessProperty.GetProperties(typeof(T));
            DataTable           dataTable  = new DataTable();

            foreach (PropertyInfo current in properties)
            {
                DataColumn dataColumn = new DataColumn();
                dataColumn.ColumnName   = current.Name;
                dataColumn.DataType     = current.PropertyType;
                dataColumn.DefaultValue = null;
                dataTable.Columns.Add(dataColumn);
            }
            foreach (object obj in objects)
            {
                DataRow dataRow = dataTable.NewRow();
                foreach (PropertyInfo current2 in properties)
                {
                    object value = current2.GetValue(obj, null);
                    dataRow[current2.Name] = value;
                }
                dataTable.Rows.Add(dataRow);
            }
            return(dataTable);
        }
예제 #4
0
        public static List <PropertyInfo> GetProperties(Type objType)
        {
            List <PropertyInfo> list = PropertyInfoCache.GetCache(objType.Name);

            if (list == null)
            {
                list = BusinessProperty.LoadPropertyMappingInfo(objType);
                PropertyInfoCache.SetCache(objType.Name, list);
            }
            return(list);
        }