Example #1
0
        /// <summary>
        /// 根据映射方式获取实体或匿名对象的字段以及对应值的 Dictionary 列表
        /// </summary>
        /// <param name="item">实体或匿名实体</param>
        /// <param name="smartMappingType">映射方式</param>
        /// <returns>字段或属性名称,值列表</returns>
        public static Dictionary <string, object> GetColumnDic(object item, FrameMappingType smartMappingType)
        {
            Type itemType = item.GetType();
            Dictionary <string, object> list = new Dictionary <string, object>();

            switch (smartMappingType)
            {
            case FrameMappingType.FrameAttributePublic:         ////公有特性
            case FrameMappingType.FrameAttributePrivate:        ////私有特性
            case FrameMappingType.FrameAttribute:               ////根据特性映射,不区分公有私有
            {
                ////根据特性标识设置属性
                foreach (PropertyInfo propertyInfo in FrameReflection.GetPropertyInfoByAttribute <FrameColumnMappingAttributes>(itemType, smartMappingType))
                {
                    FrameColumnMappingAttributes attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(FrameColumnMappingAttributes), false) as FrameColumnMappingAttributes;
                    object value = propertyInfo.GetValue(item, null);
                    list.Add(attribute.ColumnName, value);
                }
                ////根据特性标识设置字段
                foreach (FieldInfo fieldInfo in FrameReflection.GetFieldInfoByAttribute <FrameColumnMappingAttributes>(itemType, smartMappingType))
                {
                    FrameColumnMappingAttributes attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(FrameColumnMappingAttributes), false) as FrameColumnMappingAttributes;
                    object value = fieldInfo.GetValue(item);
                    list.Add(attribute.ColumnName, value);
                }
            }

            break;

            case FrameMappingType.XmlElementPublic:         ////公有XmlElement
            case FrameMappingType.XmlElementPrivate:        ////私有XmlElement
            case FrameMappingType.XmlElement:               ////根据XmlElement映射,不区分公有私有
            {
                ////根据特性标识设置属性
                foreach (PropertyInfo propertyInfo in FrameReflection.GetPropertyInfoByAttribute <System.Xml.Serialization.XmlElementAttribute>(itemType, smartMappingType))
                {
                    System.Xml.Serialization.XmlElementAttribute attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(System.Xml.Serialization.XmlElementAttribute), false) as System.Xml.Serialization.XmlElementAttribute;
                    object value = propertyInfo.GetValue(item, null);
                    list.Add(attribute.ElementName, value);
                }
                ////根据特性标识设置字段
                foreach (FieldInfo fieldInfo in FrameReflection.GetFieldInfoByAttribute <System.Xml.Serialization.XmlElementAttribute>(itemType, smartMappingType))
                {
                    System.Xml.Serialization.XmlElementAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(System.Xml.Serialization.XmlElementAttribute), false) as System.Xml.Serialization.XmlElementAttribute;
                    object value = fieldInfo.GetValue(item);
                    list.Add(attribute.ElementName, value);
                }
            }

            break;

            case FrameMappingType.Field:                ////根据字段映射,私有公有均可
            case FrameMappingType.FieldPrivate:         ////根据私有字段映射
            case FrameMappingType.FieldPublic:          ////根据公有字段映射
            {
                foreach (FieldInfo fieldInfo in FrameReflection.GetFieldInfos(itemType, smartMappingType))
                {
                    object value = fieldInfo.GetValue(item);
                    list.Add(fieldInfo.Name, value);
                }
            }

            break;

            case FrameMappingType.Property:                 ////根据属性映射   私有公有均可
            case FrameMappingType.PropertyPublic:           ////根据公有属性映射
            case FrameMappingType.ProertyPrivate:           ////根据私有属性映射
            {
                foreach (PropertyInfo propertyInfo in FrameReflection.GetPropertyInfos(itemType, smartMappingType))
                {
                    object value = propertyInfo.GetValue(item, null);
                    list.Add(propertyInfo.Name, value);
                }
            }

            break;
            }

            return(list);
        }
Example #2
0
        /// <summary>
        /// 由一行数据生成一个实体
        /// </summary>
        /// <typeparam name="T">目标实体类型</typeparam>
        /// <param name="dataRow">数据行</param>
        /// <param name="smartMappingType">反射方式</param>
        /// <param name="item">已实例化的实体</param>
        /// <param name="propertyInfos">属性</param>
        /// <param name="fieldInfos">字段</param>
        private static void GetEntity <T>(this DataRow dataRow, FrameMappingType smartMappingType, ref T item, PropertyInfo[] propertyInfos = null, FieldInfo[] fieldInfos = null)
        {
            Type itemType = item.GetType();

            switch (smartMappingType)
            {
            case FrameMappingType.FrameAttributePublic:         ////在公有字段或属性中查找特性
            case FrameMappingType.FrameAttributePrivate:        ////在私有字段或属性中查找特性
            case FrameMappingType.FrameAttribute:               ////在所有字段或属性中查找特性
            {
                ////设置属性的值

                foreach (PropertyInfo propertyInfo in propertyInfos = propertyInfos != null ? propertyInfos : FrameReflection.GetPropertyInfoByAttribute <FrameColumnMappingAttributes>(itemType, smartMappingType))
                {
                    FrameColumnMappingAttributes attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(FrameColumnMappingAttributes), false) as FrameColumnMappingAttributes;
                    if (dataRow.Table.Columns.Contains(attribute.ColumnName))
                    {
                        object value = dataRow[attribute.ColumnName];
                        if (value == null || value.ToString().Trim() == string.Empty)
                        {
                            propertyInfo.SetValue(item, attribute.DefaultVale, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(item, (value.GetType() == propertyInfo.PropertyType ? value : FrameReflection.ConvertTo(value.ToString().Trim(), propertyInfo.PropertyType)), null);
                        }
                    }
                    else
                    {
                        propertyInfo.SetValue(item, attribute.DefaultVale, null);
                    }
                }
                ////设置字段的值
                foreach (FieldInfo fieldInfo in fieldInfos = fieldInfos != null ? fieldInfos : FrameReflection.GetFieldInfoByAttribute <FrameColumnMappingAttributes>(itemType, smartMappingType))
                {
                    FrameColumnMappingAttributes attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(FrameColumnMappingAttributes), false) as FrameColumnMappingAttributes;
                    if (dataRow.Table.Columns.Contains(attribute.ColumnName))
                    {
                        object value = dataRow[attribute.ColumnName];
                        if (value == null || value.ToString().Trim() == string.Empty)
                        {
                            fieldInfo.SetValue(item, attribute.DefaultVale);
                        }
                        else
                        {
                            fieldInfo.SetValue(item, (value.GetType() == fieldInfo.FieldType ? value : FrameReflection.ConvertTo(value.ToString().Trim(), fieldInfo.FieldType)));
                        }
                    }
                    else
                    {
                        fieldInfo.SetValue(item, attribute.DefaultVale);
                    }
                }
            }

            break;

            case FrameMappingType.XmlElement:
            case FrameMappingType.XmlElementPrivate:
            case FrameMappingType.XmlElementPublic:
            {
                ////设置属性的值
                foreach (PropertyInfo propertyInfo in propertyInfos = propertyInfos != null ? propertyInfos : FrameReflection.GetPropertyInfoByAttribute <System.Xml.Serialization.XmlElementAttribute>(itemType, smartMappingType))
                {
                    System.Xml.Serialization.XmlElementAttribute attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(System.Xml.Serialization.XmlElementAttribute), false) as System.Xml.Serialization.XmlElementAttribute;
                    if (dataRow.Table.Columns.Contains(attribute.ElementName))
                    {
                        object value = dataRow[attribute.ElementName];
                        if (value == null || value.ToString().Trim() == string.Empty)
                        {
                            propertyInfo.SetValue(item, null, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(item, (value.GetType() == propertyInfo.PropertyType ? value : FrameReflection.ConvertTo(value.ToString().Trim(), propertyInfo.PropertyType)), null);
                        }
                    }
                    else
                    {
                        propertyInfo.SetValue(item, null, null);
                    }
                }
                ////设置字段的值
                foreach (FieldInfo fieldInfo in fieldInfos = fieldInfos != null ? fieldInfos : FrameReflection.GetFieldInfoByAttribute <System.Xml.Serialization.XmlElementAttribute>(itemType, smartMappingType))
                {
                    System.Xml.Serialization.XmlElementAttribute attribute = Attribute.GetCustomAttribute(fieldInfo, typeof(System.Xml.Serialization.XmlElementAttribute), false) as System.Xml.Serialization.XmlElementAttribute;
                    if (dataRow.Table.Columns.Contains(attribute.ElementName))
                    {
                        object value = dataRow[attribute.ElementName];
                        if (value == null || value.ToString().Trim() == string.Empty)
                        {
                            fieldInfo.SetValue(item, null);
                        }
                        else
                        {
                            fieldInfo.SetValue(item, (value.GetType() == fieldInfo.FieldType ? value : FrameReflection.ConvertTo(value.ToString().Trim(), fieldInfo.FieldType)));
                        }
                    }
                    else
                    {
                        fieldInfo.SetValue(item, null);
                    }
                }
            }

            break;

            case FrameMappingType.Field:        ////按字段映射,不区分公有私有
            case FrameMappingType.FieldPrivate: ////按私有字段映射
            case FrameMappingType.FieldPublic:  ////按公有字段映射
            {
                ////设置字段的值
                foreach (FieldInfo fieldInfo in fieldInfos = fieldInfos != null ? fieldInfos : FrameReflection.GetFieldInfos(itemType, smartMappingType))
                {
                    if (dataRow.Table.Columns.Contains(fieldInfo.Name))
                    {
                        object value = dataRow[fieldInfo.Name];
                        if (value != null && value.ToString().Trim() != string.Empty)
                        {
                            fieldInfo.SetValue(item, (value.GetType() == fieldInfo.FieldType ? value : FrameReflection.ConvertTo(value.ToString(), fieldInfo.FieldType)));
                        }
                    }
                }
            }

            break;

            case FrameMappingType.Property:
            case FrameMappingType.ProertyPrivate:
            case FrameMappingType.PropertyPublic:
            {
                ////设置属性的值
                foreach (PropertyInfo propertyInfo in propertyInfos = propertyInfos != null ? propertyInfos : FrameReflection.GetPropertyInfos(itemType, smartMappingType))
                {
                    if (dataRow.Table.Columns.Contains(propertyInfo.Name))
                    {
                        object value = dataRow[propertyInfo.Name];
                        if (value != null && value.ToString().Trim() != string.Empty)
                        {
                            propertyInfo.SetValue(item, (value.GetType() == propertyInfo.PropertyType ? value : FrameReflection.ConvertTo(value.ToString(), propertyInfo.PropertyType)), null);
                        }
                    }
                }
            }

            break;
            }
        }