CallSiteRuntimeResolver是实现了CallSiteVisitor之一。
提供的方法主要分三个部分
自有成员方法
重写父类方法
VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
调用父类VisitCallSiteMain ,并把创建出来的服务添加到context.Scope.CaptureDisposable
return context.Scope.CaptureDisposable(VisitCallSiteMain(transientCallSite, context));object VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
if (callSite.Value is object value) { return value; }var lockType = RuntimeResolverLock.Root;ServiceProviderEngineScope serviceProviderEngine = context.Scope.RootProvider.Root;if (callSite.Value is object resolved){ return resolved;}resolved = VisitCallSiteMain(callSite, new RuntimeResolverContext{ Scope = serviceProviderEngine, AcquiredLocks = context.AcquiredLocks | lockType});serviceProviderEngine.CaptureDisposable(resolved);callSite.Value = resolved;return resolved;VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
if (resolvedServices.TryGetValue(callSite.Cache.Key, out object resolved)) { return resolved; } resolved = VisitCallSiteMain(callSite, new RuntimeResolverContext { Scope = serviceProviderEngine, AcquiredLocks = context.AcquiredLocks | lockType }); serviceProviderEngine.CaptureDisposable(resolved); resolvedServices.Add(callSite.Cache.Key, resolved); return resolved;实现父类抽象方法
object[] parameterValues; if (constructorCallSite.ParameterCallSites.Length == 0) { parameterValues = Array.Empty<object>(); } else { parameterValues = new object[constructorCallSite.ParameterCallSites.Length]; for (int index = 0; index < parameterValues.Length; index++) { parameterValues[index] = VisitCallSite(constructorCallSite.ParameterCallSites[index], context); } } return constructorCallSite.ConstructorInfo.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: parameterValues, culture: null); return constantCallSite.DefaultValue;return context.Scope; var array = Array.CreateInstance( enumerableCallSite.ItemType, enumerableCallSite.ServiceCallSites.Length); for (int index = 0; index < enumerableCallSite.ServiceCallSites.Length; index++) { object value = VisitCallSite(enumerableCallSite.ServiceCallSites[index], context); array.SetValue(value, index); } return array; return factoryCallSite.Factory(context.Scope);体力活都给CallSiteFactory了
VisitCallSite(ServiceCallSite callSite, TArgument argument)
switch (callSite.Cache.Location) { case CallSiteResultCacheLocation.Root: return VisitRootCache(callSite, argument); case CallSiteResultCacheLocation.Scope: return VisitScopeCache(callSite, argument); case CallSiteResultCacheLocation.Dispose: return VisitDisposeCache(callSite, argument); case CallSiteResultCacheLocation.None: return VisitNoCache(callSite, argument); default: throw new ArgumentOutOfRangeException(); }VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
switch (callSite.Kind) { case CallSiteKind.Factory: return VisitFactory((FactoryCallSite)callSite, argument); case CallSiteKind.IEnumerable: return VisitIEnumerable((IEnumerableCallSite)callSite, argument); case CallSiteKind.Constructor: return VisitConstructor((ConstructorCallSite)callSite, argument); case CallSiteKind.Constant: return VisitConstant((ConstantCallSite)callSite, argument); case CallSiteKind.ServiceProvider: return VisitServiceProvider((ServiceProviderCallSite)callSite, argument); default: throw new NotSupportedException(SR.Format(SR.CallSiteTypeNotSupported, callSite.GetType())); }根据生命周期策略由子类实现对应存储机制。
根据服务注册时的创建机制策略由子类实现对应的创建对象机制。
CallSiteVisitor负责这活怎么干!。CallSiteRuntimeResolver负责干活
CallSiteRuntimeResolver.Resolve -> CallSiteVisitor->VisitCallSite callSite.Cache.Location VisitRootCache/VisitScopeCache/ VisitDisposeCache/ VisitNoCache -> CallSiteRuntimeResolver.VisitCache/ VisitRootCache -> VisitRootCache -> CallSiteVisitor.VisitCallSiteMain 根据callSite.Kind CallSiteRuntimeResolver.VisitFactory(abstract TResult VisitFactory)递归调用 VisitCallSite
文章中提到的代码,请在source.dot.net快速搜索预览
本文来自博客园,作者:一身大膘,转载请注明原文链接:https://www.cnblogs.com/hts92/p/15812761.html