Пример #1
0
        private static object CreateInstance(Type type)
        {
            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsInterface || typeInfo.IsAbstract)
            {
                throw new InvalidOperationException(Resources.FormatError_CannotActivateAbstractOrInterface(type));
            }

            if (typeInfo.IsArray)
            {
                if (typeInfo.GetArrayRank() > 1)
                {
                    throw new InvalidOperationException(Resources.FormatError_UnsupportedMultidimensionalArray(type));
                }

                return(Array.CreateInstance(typeInfo.GetElementType(), 0));
            }

            var hasDefaultConstructor = typeInfo.DeclaredConstructors.Any(ctor => ctor.IsPublic && ctor.GetParameters().Length == 0);

            if (!hasDefaultConstructor)
            {
                throw new InvalidOperationException(Resources.FormatError_MissingParameterlessConstructor(type));
            }

            try
            {
                return(Activator.CreateInstance(type));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(Resources.FormatError_FailedToActivate(type), ex);
            }
        }
Пример #2
0
        private static object BindInstance(Type type, object instance, IConfiguration config)
        {
            var section     = config as IConfigurationSection;
            var configValue = section?.Value;

            if (configValue != null)
            {
                // Leaf nodes are always reinitialized
                return(ReadValue(type, configValue, section));
            }
            else
            {
                if (config.GetChildren().Count() != 0)
                {
                    if (instance == null)
                    {
                        var typeInfo = type.GetTypeInfo();
                        if (typeInfo.IsInterface || typeInfo.IsAbstract)
                        {
                            throw new InvalidOperationException(Resources.FormatError_CannotActivateAbstractOrInterface(type));
                        }

                        var hasDefaultConstructor = typeInfo.DeclaredConstructors.Any(ctor => ctor.IsPublic && ctor.GetParameters().Length == 0);
                        if (!hasDefaultConstructor)
                        {
                            throw new InvalidOperationException(Resources.FormatError_MissingParameterlessConstructor(type));
                        }

                        try
                        {
                            instance = Activator.CreateInstance(type);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidOperationException(Resources.FormatError_FailedToActivate(type), ex);
                        }
                    }

                    // See if its a Dictionary
                    var collectionInterface = FindOpenGenericInterface(typeof(IDictionary <,>), type);
                    if (collectionInterface != null)
                    {
                        BindDictionary(instance, collectionInterface, config);
                    }
                    else
                    {
                        // See if its an ICollection
                        collectionInterface = FindOpenGenericInterface(typeof(ICollection <>), type);
                        if (collectionInterface != null)
                        {
                            BindCollection(instance, collectionInterface, config);
                        }
                        // Something else
                        else
                        {
                            Bind(config, instance);
                        }
                    }
                }
                return(instance);
            }
        }
Пример #3
0
        private static object BindType(Type type, object typeInstance, IConfiguration configuration)
        {
            var configValue = configuration.Get(null);
            var typeInfo    = type.GetTypeInfo();

            if (configValue != null)
            {
                // Leaf nodes are always reinitialized
                return(CreateValueFromConfiguration(type, configValue, configuration));
            }
            else
            {
                var subkeys = configuration.GetConfigurationSections();
                if (subkeys.Count() != 0)
                {
                    if (typeInstance == null)
                    {
                        if (typeInfo.IsInterface || typeInfo.IsAbstract)
                        {
                            throw new InvalidOperationException(Resources.FormatError_CannotActivateAbstractOrInterface(type));
                        }

                        bool hasParameterlessConstructor = typeInfo.DeclaredConstructors.Any(ctor => ctor.IsPublic && ctor.GetParameters().Length == 0);
                        if (!hasParameterlessConstructor)
                        {
                            throw new InvalidOperationException(Resources.FormatError_MissingParameterlessConstructor(type));
                        }

                        try
                        {
                            typeInstance = Activator.CreateInstance(type);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidOperationException(Resources.FormatError_FailedToActivate(type), ex);
                        }
                    }

                    var collectionInterface = GetGenericOpenInterfaceImplementation(typeof(IDictionary <,>), type);
                    if (collectionInterface != null)
                    {
                        // Dictionary
                        BindDictionary(typeInstance, collectionInterface, configuration);
                    }
                    else
                    {
                        collectionInterface = GetGenericOpenInterfaceImplementation(typeof(ICollection <>), type);
                        if (collectionInterface != null)
                        {
                            // ICollection
                            BindCollection(typeInstance, collectionInterface, configuration);
                        }
                        else
                        {
                            // Something else
                            BindObjectProperties(typeInstance, configuration);
                        }
                    }
                }
                return(typeInstance);
            }
        }