Beispiel #1
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);
            }
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }