コード例 #1
0
        private bool TryToAddToConfig(Type typeInterface, Type typeImpl, bool isSingleton)
        {
            Console.WriteLine(CanBeGeneratedByConstructor(typeImpl));
            if (!typeImpl.IsInterface && !typeImpl.IsAbstract &&
                (typeInterface.IsClass || typeInterface.IsInterface) && CanBeGeneratedByConstructor(typeImpl))
            {
                Type nestedType = null;

                if (typeInterface.IsGenericType)
                {
                    if (typeImpl.GenericTypeArguments.Length > 0)
                    {
                        nestedType = typeImpl.GenericTypeArguments[0];
                    }
                }

                if (!Dependencies.ContainsKey(typeInterface))
                {
                    var dependencies = new DependenciesImpls(isSingleton, typeImpl, nestedType);
                    Dependencies.TryAdd(typeInterface, dependencies);
                    return(true);
                }
                else
                {
                    Dependencies.TryGetValue(typeInterface, out DependenciesImpls dependencies);
                    dependencies.AddTypeToList(isSingleton, typeImpl, nestedType);
                }
            }
            return(false);
        }
コード例 #2
0
        private object CreateInstance(Type type)
        {
            object result = null;

            if (!_typesInGeneration.Contains(type))
            {
                _typesInGeneration.Add(type);
                if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IEnumerable <>)))
                {
                    result = CreateIEnumerable(type);
                    _typesInGeneration.Remove(type);
                }
                else
                {
                    if (type.IsGenericType && (type.GenericTypeArguments.Length > 0))
                    {
                        Type nestedType                     = type.GenericTypeArguments[0];
                        DependenciesImpls impls             = null;
                        DependenciesImpls implsOfNestedType = null;
                        var typeOpenGeneric                 = type.GetGenericTypeDefinition();
                        DependencyDetails generic           = null;
                        DependencyDetails nested            = null;
                        if ((_configuration.Dependencies.TryGetValue(type, out impls) || (_configuration.Dependencies.TryGetValue(type.GetGenericTypeDefinition(), out impls))) &&
                            _configuration.Dependencies.TryGetValue(nestedType, out implsOfNestedType))
                        {
                            generic = impls.ImplTypes[impls.ImplTypes.Count - 1];
                            nested  = implsOfNestedType.ImplTypes[implsOfNestedType.ImplTypes.Count - 1];
                            result  = GenerateGenericDependency(generic, nestedType);
                            _typesInGeneration.Remove(type);
                        }
                    }
                    else
                    {
                        if (_configuration.Dependencies.TryGetValue(type, out DependenciesImpls details))
                        {
                            var dependency = details.ImplTypes[details.ImplTypes.Count - 1];
                            result = GenerateDependency(dependency);
                            _typesInGeneration.Remove(type);
                        }
                    }
                }
            }
            return(result);
        }
コード例 #3
0
        private object CreateIEnumerable(Type type)
        {
            if ((type.GetGenericArguments().Length > 0))
            {
                Type interfaceType = type.GetGenericArguments()[0];
                if (type.IsGenericTypeDefinition)
                {
                    Type nestedType = interfaceType.GetGenericArguments()[0];

                    DependenciesImpls impls             = null;
                    DependenciesImpls implsOfNestedType = null;
                    if ((_configuration.Dependencies.TryGetValue(type, out impls) || (_configuration.Dependencies.TryGetValue(type.GetGenericTypeDefinition(), out impls))) &&
                        _configuration.Dependencies.TryGetValue(nestedType, out implsOfNestedType))
                    {
                        var result     = Array.CreateInstance(type, impls.ImplTypes.Count);
                        var nestedImpl = implsOfNestedType.ImplTypes[implsOfNestedType.ImplTypes.Count - 1];
                        int index      = 0;
                        foreach (var impl in impls.ImplTypes)
                        {
                            result.SetValue(GenerateGenericDependency(impl, nestedType), index);
                            index++;
                        }
                        return(result);
                    }
                }
                else if (_configuration.Dependencies.TryGetValue(type.GetGenericArguments()[0], out DependenciesImpls impls))
                {
                    var result = Array.CreateInstance(type.GetGenericArguments()[0], impls.ImplTypes.Count);

                    int index = 0;
                    foreach (var impl in impls.ImplTypes)
                    {
                        result.SetValue(GenerateDependency(impl), index);
                        index++;
                    }
                    return(result);
                }
            }
            return(null);
        }