示例#1
0
        private bool HandleComplexField(object item, DataReaderField field, object value)
        {
            string propertyName = null;

            for (var level = 0; level <= field.NestedLevels; level++)
            {
                if (string.IsNullOrEmpty(propertyName))
                    propertyName = field.GetNestedName(level);
                else
                    propertyName += "_" + field.GetNestedName(level);

                PropertyInfo property = null;
                var properties = ReflectionHelper.GetProperties(item.GetType());
                if (properties.TryGetValue(propertyName, out property))
                {
                    if (level == field.NestedLevels)
                    {
                        SetPropertyValue(field, property, item, value);
                        return true;
                    }
                    else
                    {
                        item = GetOrCreateInstance(item, property);
                        if (item == null)
                            return false;
                        propertyName = null;
                    }
                }
            }

            return false;
        }
示例#2
0
        private void SetPropertyValue(DataReaderField field, PropertyInfo property, object item, object value)
        {
            try
            {
                if (value == DBNull.Value)
                {
                    if (ReflectionHelper.IsNullable(property))
                    {
                        value = null;
                    }
                    else
                    {
                        value = ReflectionHelper.GetDefault(property.PropertyType);
                    }
                }
                else
                {
                    var propertyType = ReflectionHelper.GetPropertyType(property);

                    if (propertyType != field.Type)
                    {
                        if (propertyType.IsEnum)
                        {
                            if (field.Type == typeof(string))
                            {
                                value = Enum.Parse(propertyType, (string)value, true);
                            }
                            else
                            {
                                value = Enum.ToObject(propertyType, value);
                            }
                        }
                        else if (!ReflectionHelper.IsBasicClrType(propertyType))
                        {
                            return;
                        }
                        else if (propertyType == typeof(string))
                        {
                            value = value.ToString();
                        }
                        else
                        {
                            value = Convert.ChangeType(value, property.PropertyType);
                        }
                    }
                }

                property.SetValue(item, value, null);
            }
            catch (Exception exception)
            {
                throw new FluentDataException("Could not map: " + property.Name, exception);
            }
        }
		internal static List<DataReaderField> GetDataReaderFields(System.Data.IDataReader reader)
		{
			var columns = new List<DataReaderField>();

			for (var i = 0; i < reader.FieldCount; i++)
			{
				var column = new DataReaderField(i, reader.GetName(i), reader.GetFieldType(i));

				if (columns.SingleOrDefault(x => x.LowerName == column.LowerName) == null)
					columns.Add(column);
			}

			return columns;
		}
示例#4
0
        internal static List <DataReaderField> GetDataReaderFields(System.Data.IDataReader reader)
        {
            var columns = new List <DataReaderField>();

            for (var i = 0; i < reader.FieldCount; i++)
            {
                var column = new DataReaderField(i, reader.GetName(i), reader.GetFieldType(i));

                if (columns.SingleOrDefault(x => x.LowerName == column.LowerName) == null)
                {
                    columns.Add(column);
                }
            }

            return(columns);
        }
示例#5
0
        private bool HandleComplexField(object item, DataReaderField field, object value)
        {
            string propertyName = null;

            for (var level = 0; level <= field.NestedLevels; level++)
            {
                if (string.IsNullOrEmpty(propertyName))
                {
                    propertyName = field.GetNestedName(level);
                }
                else
                {
                    propertyName += "_" + field.GetNestedName(level);
                }

                PropertyInfo property   = null;
                var          properties = ReflectionHelper.GetProperties(item.GetType());
                if (properties.TryGetValue(propertyName, out property))
                {
                    if (level == field.NestedLevels)
                    {
                        SetPropertyValue(field, property, item, value);
                        return(true);
                    }
                    else
                    {
                        item = GetOrCreateInstance(item, property);
                        if (item == null)
                        {
                            return(false);
                        }
                        propertyName = null;
                    }
                }
            }

            return(false);
        }
示例#6
0
        private void SetPropertyValue(DataReaderField field, PropertyInfo property, object item, object value)
        {
            try
            {
                if (value == DBNull.Value)
                {
                    if (ReflectionHelper.IsNullable(property))
                        value = null;
                    else
                        value = ReflectionHelper.GetDefault(property.PropertyType);
                }
                else
                {
                    var propertyType = ReflectionHelper.GetPropertyType(property);

                    if (propertyType != field.Type)
                    {
                        if (propertyType.IsEnum)
                        {
                            if (field.Type == typeof(string))
                                value = Enum.Parse(propertyType, (string)value, true);
                            else
                                value = Enum.ToObject(propertyType, value);
                        }
                        else if (!ReflectionHelper.IsBasicClrType(propertyType))
                            return;
                        else if (propertyType == typeof(string))
                            value = value.ToString();
                        else
                            value = Convert.ChangeType(value, property.PropertyType);
                    }
                }

                property.SetValue(item, value, null);
            }
            catch (Exception exception)
            {
                throw new FluentDataException("Could not map: " + property.Name, exception);
            }
        }