为什么在这段代码中使用 Func<T>?
Why Func<T> is used in this code?
我的一位同事遇到了 this 代码,我们无法理解为什么需要 _resolverThunk
。
代码摘要如下所示:
private Func<IDependencyResolver> _resolverThunk;
public SingleServiceResolver()
{
_resolverThunk = () => DependencyResolver.Current;
}
private TService GetValueFromResolver()
{
TService result = _resolverThunk().GetService<TService>();
return result;
}
如果您查看另一个内部构造函数,它会设置 _resolverThunk
from 参数。所以这是有道理的。
internal SingleServiceResolver(Func<TService> staticAccessor, TService defaultValue, IDependencyResolver resolver, string callerMethodName)
: this(staticAccessor, defaultValue, callerMethodName)
{
if (resolver != null)
{
_resolverThunk = () => resolver;
}
}
否则,没有理由去做。
我的一位同事遇到了 this 代码,我们无法理解为什么需要 _resolverThunk
。
代码摘要如下所示:
private Func<IDependencyResolver> _resolverThunk;
public SingleServiceResolver()
{
_resolverThunk = () => DependencyResolver.Current;
}
private TService GetValueFromResolver()
{
TService result = _resolverThunk().GetService<TService>();
return result;
}
如果您查看另一个内部构造函数,它会设置 _resolverThunk
from 参数。所以这是有道理的。
internal SingleServiceResolver(Func<TService> staticAccessor, TService defaultValue, IDependencyResolver resolver, string callerMethodName)
: this(staticAccessor, defaultValue, callerMethodName)
{
if (resolver != null)
{
_resolverThunk = () => resolver;
}
}
否则,没有理由去做。