DI容器如何知道构造函数需要什么(ASP.NET核心)?

Dmi*_*rov 2 c# dependency-injection .net-core asp.net-core

我已经阅读了很多关于什么是DI以及如何使用它的文档(与ASP.NET Core相关).据我所知,当框架为我实例化一些控制器时,它以某种方式知道控制器的类需要传递给构造函数.是反射还是什么?有人可以告诉我在ASP.NET Core GitHub源代码中我可以在哪里看到它吗?

Nko*_*osi 8

你可以在GitHub上看到这里.

在一个坚果壳中,它使用反射来检查类型及其参数的公共构造函数.

var constructors = implementationType.GetTypeInfo()
    .DeclaredConstructors
    .Where(constructor => constructor.IsPublic)
    .ToArray();
Run Code Online (Sandbox Code Playgroud)

它根据参数长度对构造函数进行排序,然后选择最佳构造函数.

此代码段查找要调用实例化类型的最佳构造函数.

private ServiceCallSite CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType,
    CallSiteChain callSiteChain)
{
    try
    {
        callSiteChain.Add(serviceType, implementationType);
        var constructors = implementationType.GetTypeInfo()
            .DeclaredConstructors
            .Where(constructor => constructor.IsPublic)
            .ToArray();

        ServiceCallSite[] parameterCallSites = null;

        if (constructors.Length == 0)
        {
            throw new InvalidOperationException(Resources.FormatNoConstructorMatch(implementationType));
        }
        else if (constructors.Length == 1)
        {
            var constructor = constructors[0];
            var parameters = constructor.GetParameters();
            if (parameters.Length == 0)
            {
                return new ConstructorCallSite(lifetime, serviceType, constructor);
            }

            parameterCallSites = CreateArgumentCallSites(
                serviceType,
                implementationType,
                callSiteChain,
                parameters,
                throwIfCallSiteNotFound: true);

            return new ConstructorCallSite(lifetime, serviceType, constructor, parameterCallSites);
        }

        Array.Sort(constructors,
            (a, b) => b.GetParameters().Length.CompareTo(a.GetParameters().Length));

        ConstructorInfo bestConstructor = null;
        HashSet<Type> bestConstructorParameterTypes = null;
        for (var i = 0; i < constructors.Length; i++)
        {
            var parameters = constructors[i].GetParameters();

            var currentParameterCallSites = CreateArgumentCallSites(
                serviceType,
                implementationType,
                callSiteChain,
                parameters,
                throwIfCallSiteNotFound: false);

            if (currentParameterCallSites != null)
            {
                if (bestConstructor == null)
                {
                    bestConstructor = constructors[i];
                    parameterCallSites = currentParameterCallSites;
                }
                else
                {
                    // Since we're visiting constructors in decreasing order of number of parameters,
                    // we'll only see ambiguities or supersets once we've seen a 'bestConstructor'.

                    if (bestConstructorParameterTypes == null)
                    {
                        bestConstructorParameterTypes = new HashSet<Type>(
                            bestConstructor.GetParameters().Select(p => p.ParameterType));
                    }

                    if (!bestConstructorParameterTypes.IsSupersetOf(parameters.Select(p => p.ParameterType)))
                    {
                        // Ambiguous match exception
                        var message = string.Join(
                            Environment.NewLine,
                            Resources.FormatAmbiguousConstructorException(implementationType),
                            bestConstructor,
                            constructors[i]);
                        throw new InvalidOperationException(message);
                    }
                }
            }
        }

        if (bestConstructor == null)
        {
            throw new InvalidOperationException(
                Resources.FormatUnableToActivateTypeException(implementationType));
        }
        else
        {
            Debug.Assert(parameterCallSites != null);
            return new ConstructorCallSite(lifetime, serviceType, bestConstructor, parameterCallSites);
        }
    }
    finally
    {
        callSiteChain.Remove(serviceType);
    }
}
Run Code Online (Sandbox Code Playgroud)