Example #1
0
        public bool TryGetSettings <T>(string prefix, out T settings)
            where T : ISettings
        {
            IObjectConverter converter = _converterCache.GetConverter(typeof(T));

            var provider = new ConfigurationObjectValueProvider(_configurationProvider, prefix);

            settings = (T)converter.GetObject(provider);
            return(true);
        }
        /// <inheritdoc />
        public virtual T MapObject <T>(string prefix, IDictionary dictionary)
        {
            dictionary = dictionary ?? new Hashtable();
            var provider = new DictionaryObjectValueProvider(dictionary, prefix);

            var converter = _objectConverterCache.GetConverter(typeof(T));
            var obj       = (T)converter.GetObject(provider);

            return(obj);
        }
        IObjectMapper <TImplementation> GetDictionaryToObjectConverter(ReadWriteProperty <TImplementation> property, Type valueType)
        {
            var underlyingType = Nullable.GetUnderlyingType(valueType);

            if (underlyingType != null)
            {
                var converterType = typeof(NullableValueObjectMapper <,>).MakeGenericType(typeof(TImplementation), underlyingType);

                return((IObjectMapper <TImplementation>)Activator.CreateInstance(converterType, property));
            }

            if (valueType.GetTypeInfo().IsEnum)
            {
                return(new EnumObjectMapper <TImplementation>(property));
            }

            if (valueType.IsArray)
            {
                var elementType = valueType.GetElementType();
                if (ValueObject.IsValueObjectType(elementType))
                {
                    var valueConverterType = typeof(ValueArrayObjectMapper <,>).MakeGenericType(typeof(TImplementation), elementType);
                    return((IObjectMapper <TImplementation>)Activator.CreateInstance(valueConverterType, property));
                }

                var elementConverter = _cache.GetConverter(elementType);

                var converterType = typeof(ObjectArrayObjectMapper <,>).MakeGenericType(typeof(TImplementation), elementType);
                return((IObjectMapper <TImplementation>)Activator.CreateInstance(converterType, property, elementConverter));
            }

            if (ValueObject.IsValueObjectType(valueType))
            {
                return(new ValueObjectMapper <TImplementation>(property));
            }

            if (valueType.ClosesType(typeof(IEnumerable <>)))
            {
                if (valueType.ClosesType(typeof(IDictionary <,>)))
                {
                    Type[] genericArguments = valueType.GetClosingArguments(typeof(IDictionary <,>)).ToArray();

                    var keyType     = genericArguments[0];
                    var elementType = genericArguments[1];


                    if (ValueObject.IsValueObjectType(keyType))
                    {
                        if (ValueObject.IsValueObjectType(elementType))
                        {
                            var valueConverterType = typeof(ValueValueDictionaryObjectMapper <, ,>).MakeGenericType(typeof(TImplementation), keyType, elementType);
                            return((IObjectMapper <TImplementation>)Activator.CreateInstance(valueConverterType, property));
                        }
                        else
                        {
                            var elementConverter   = _cache.GetConverter(elementType);
                            var valueConverterType = typeof(ValueObjectDictionaryObjectMapper <, ,>).MakeGenericType(typeof(TImplementation), keyType, elementType);
                            return((IObjectMapper <TImplementation>)Activator.CreateInstance(valueConverterType, property, elementConverter));
                        }
                    }

                    throw new InvalidOperationException("A dictionary with a reference type key is not supported: " + property.Property.Name);
                }

                if (valueType.ClosesType(typeof(IList <>)) || valueType.ClosesType(typeof(IEnumerable <>)))
                {
                    Type[] genericArguments = valueType.GetClosingArguments(typeof(IEnumerable <>)).ToArray();
                    var    elementType      = genericArguments[0];

                    if (ValueObject.IsValueObjectType(elementType))
                    {
                        var valueConverterType = typeof(ValueListObjectMapper <,>).MakeGenericType(typeof(TImplementation), elementType);

                        return((IObjectMapper <TImplementation>)Activator.CreateInstance(valueConverterType, property));
                    }

                    var elementConverter = _cache.GetConverter(elementType);

                    var converterType = typeof(ObjectListObjectMapper <,>).MakeGenericType(typeof(TImplementation), elementType);

                    return((IObjectMapper <TImplementation>)Activator.CreateInstance(converterType, property, elementConverter));
                }
            }

            return(new ObjectObjectMapper <TImplementation>(property, _cache.GetConverter(valueType)));
        }
Example #4
0
 public IObjectConverter GetObjectConverter(Type type)
 {
     return(_dtoCache.GetConverter(type));
 }