Exemple #1
0
        public object CreateObject(Type type)
        {
            if (type.IsOfRawGenericTypeDefinition(typeof(IDictionary <,>)))
            {
                var args = type.GetGenericArguments();
                type = typeof(Dictionary <,>).MakeGenericType(args);
                return(CreateObject(type));
            }

            if (type.IsOfRawGenericTypeDefinition(typeof(IList <>)))
            {
                type = typeof(List <>).MakeGenericType(type.GetGenericArguments());
                return(CreateObject(type));
            }
            if (type.IsOfRawGenericTypeDefinition(typeof(ICollection <>)))
            {
                type = typeof(Collection <>).MakeGenericType(type.GetGenericArguments()[0]);
                return(CreateObject(type));
            }

            if (type.IsOfRawGenericTypeDefinition(typeof(IEnumerable <>)))
            {
                return(CreateArrayObject(type.GetGenericArguments()[0]));
            }

            if (type.IsArray)
            {
                return(CreateArrayObject(type.GetElementType()));
            }
            var f = configuration.GetFactory(type);

            if (f != null)
            {
                return(f(type));
            }
            if (type.IsEnum)
            {
                f = configuration.GetFactory(typeof(Enum));
                if (f != null)
                {
                    return(f(type));
                }
            }
            return(GetNewObject(type) ?? type.GetDefault());
        }
        Func <Type, object> IAutoConfigurationResolver.GetFactory(Type type, bool cascade)
        {
            var key = type.CreateKey();
            Func <Type, object> value;

            if (factoryDictionary.TryGetValue(key, out value))
            {
                return(value);
            }
            if (cascade && configuration != null)
            {
                return(configuration.GetFactory(type));
            }
            return(null);
        }