Exemple #1
0
        public static DataTable GenerateEmptyDataTable
        (
            Type type
            , bool needDefinitionAttributeProcess = false
        )
        {
            MemberAdditionalDefinitionAttribute attribute = null;
            var members = TypeHelper
                          .GetModelMembers(type)
                          .OrderBy
                          (
                (x) =>
            {
                int r     = int.MinValue;
                attribute = x
                            .GetCustomAttributes
                                (typeof(MemberAdditionalDefinitionAttribute), true)
                            .FirstOrDefault() as MemberAdditionalDefinitionAttribute;
                if (attribute != null)
                {
                    if (attribute.DataTableColumnID.HasValue)
                    {
                        r = attribute.DataTableColumnID.Value;
                    }
                }
                return(r);
            }
                          );
            DataTable            dataTable             = null;
            DataColumnCollection dataColumnsCollection = null;

            foreach (var x in members)
            {
                if (dataTable == null)
                {
                    dataTable = new DataTable();
                    if (dataColumnsCollection == null)
                    {
                        dataColumnsCollection = dataTable.Columns;
                    }
                }
                var dataColumnName = x.Name;
                if (needDefinitionAttributeProcess)
                {
                    if (attribute != null)
                    {
                        if (!attribute.DataTableColumnName.IsNullOrEmptyOrWhiteSpace())
                        {
                            dataColumnName = attribute.DataTableColumnName;
                        }
                    }
                }
                Type dataColumnType = null;
                if (x is FieldInfo)
                {
                    var fieldInfo = x as FieldInfo;
                    dataColumnType = fieldInfo.FieldType;
                }
                else if (x is PropertyInfo)
                {
                    var propertyInfo = x as PropertyInfo;
                    dataColumnType = propertyInfo.PropertyType;
                }
                if (needDefinitionAttributeProcess)
                {
                    if (attribute != null)
                    {
                        if (attribute.DataTableColumnDataType != null)
                        {
                            dataColumnType = attribute.DataTableColumnDataType;
                        }
                    }
                }
                if (dataColumnType.IsNullableType())
                {
                    dataColumnType = TypeHelper.GetNullableUnderlyingType(dataColumnType);
                }

                dataColumnsCollection
                .Add
                (
                    dataColumnName
                    , dataColumnType
                );
            }
            return(dataTable);
        }
        public static IEnumerable <TEntry> GetEnumerable <TEntry>
        (
            IDataReader dataReader
            , bool needDefinitionAttributeProcess = false
        )
            where TEntry : new()
        {
            var type = typeof(TEntry);
            MemberAdditionalDefinitionAttribute attribute = null;
            var members = TypeHelper
                          .GetModelMembers(type)
                          .Select
                          (
                (x) =>
            {
                attribute =
                    x
                    .GetCustomAttributes
                        (typeof(MemberAdditionalDefinitionAttribute), true)
                    .FirstOrDefault() as MemberAdditionalDefinitionAttribute;
                return(x);
            }
                          );

            while (dataReader.Read())
            {
                TEntry entry = new TEntry();
                foreach (var x in members)
                {
                    var dataColumnName = x.Name;
                    if (needDefinitionAttributeProcess)
                    {
                        if (attribute != null)
                        {
                            if
                            (
                                !attribute
                                .DataTableColumnName
                                .IsNullOrEmptyOrWhiteSpace()
                            )
                            {
                                dataColumnName = attribute.DataTableColumnName;
                            }
                        }
                    }
                    var setter = DynamicExpressionTreeHelper
                                 .CreateMemberSetter <TEntry, object>
                                 (
                        x.Name
                                 );
                    setter
                    (
                        entry
                        , dataReader[dataColumnName]
                    );
                }
                yield
                return
                    (entry);
            }
        }