我的一位同事遇到了this代码,我们无法理解为什么需要_resolverThunk
代码摘要如下所示:

private Func<IDependencyResolver> _resolverThunk;

public SingleServiceResolver()
{
    _resolverThunk = () => DependencyResolver.Current;
}

private TService GetValueFromResolver()
{
   TService result = _resolverThunk().GetService<TService>();
   return result;
}

最佳答案

如果您查看另一个内部构造函数,它将设置_resolverThunkfrom参数。所以这是有道理的。

 internal SingleServiceResolver(Func<TService> staticAccessor, TService defaultValue, IDependencyResolver resolver, string callerMethodName)
        : this(staticAccessor, defaultValue, callerMethodName)
 {
     if (resolver != null)
     {
            _resolverThunk = () => resolver;
     }
 }

否则,就没有理由这么做。

10-06 06:09