gpt4 book ai didi

c# - DI 容器如何知道构造函数需要什么(ASP.NET Core)?

转载 作者:行者123 更新时间:2023-11-30 19:15:34 25 4
gpt4 key购买 nike

我已经阅读了很多关于什么是 DI 以及如何使用它的文档(与 ASP.NET Core 相关)。据我了解,当框架为我实例化某个 Controller 时,它以某种方式知道该 Controller 的类需要传递给构造函数的内容。是反射还是什么?谁能告诉我在 ASP.NET Core GitHub 资源上哪里可以看到它?

最佳答案

你可以开始寻找here在 GitHub 上。

简而言之,它使用反射来检查类型的公共(public)构造函数及其参数。

var constructors = implementationType.GetTypeInfo()
.DeclaredConstructors
.Where(constructor => constructor.IsPublic)
.ToArray();

它根据参数长度对构造函数进行排序,然后选择最好的一个。

此代码片段寻找最佳构造函数来调用被实例化的类型。

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);
}
}

关于c# - DI 容器如何知道构造函数需要什么(ASP.NET Core)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com