private IServiceCallSite[] CreateArgumentCallSites( Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new IServiceCallSite[parameters.Length]; for (var index = 0; index < parameters.Length; index++) { var callSite = CreateCallSite(parameters[index].ParameterType, callSiteChain); if (callSite == null && ParameterDefaultValue.TryGetDefaultValue(parameters[index], out var defaultValue)) { callSite = new ConstantCallSite(serviceType, defaultValue); } if (callSite == null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(Resources.FormatCannotResolveService( parameters[index].ParameterType, implementationType)); } return(null); } parameterCallSites[index] = callSite; } return(parameterCallSites); }
private IServiceCallSite TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain) { if (serviceType == descriptor.ServiceType) { IServiceCallSite callSite; if (descriptor.ImplementationInstance != null) { callSite = new ConstantCallSite(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory != null) { callSite = new FactoryCallSite(descriptor.ServiceType, descriptor.ImplementationFactory); } else if (descriptor.ImplementationType != null) { callSite = CreateConstructorCallSite(descriptor.ServiceType, descriptor.ImplementationType, callSiteChain); } else { throw new InvalidOperationException("Invalid service descriptor"); } callSite = new InterceptionCallSite(_interceptingProxyFactory, callSite); return(ApplyLifetime(callSite, descriptor, descriptor.Lifetime)); } return(null); }
private ServiceCallSite TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot) { if (serviceType == descriptor.ServiceType) { ServiceCacheKey callSiteKey = new ServiceCacheKey(serviceType, slot); if (_callSiteCache.TryGetValue(callSiteKey, out ServiceCallSite serviceCallSite)) { return(serviceCallSite); } ServiceCallSite callSite; var lifetime = new ResultCache(descriptor.Lifetime, serviceType, slot); if (descriptor.ImplementationInstance != null) { callSite = new ConstantCallSite(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory != null) { callSite = new FactoryCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationFactory); } else if (descriptor.ImplementationType != null) { callSite = CreateConstructorCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationType, callSiteChain); } else { throw new InvalidOperationException(SR.InvalidServiceDescriptor); } return(_callSiteCache[callSiteKey] = callSite); } return(null); }
private ServiceCallSite[] CreateArgumentCallSites( Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new List <ServiceCallSite>(parameters.Length); foreach (var item in parameters) { var callSite = GetCallSite(item.ParameterType, callSiteChain); if (callSite is null && TryGetDefaultValue(item, out var defaultValue)) { callSite = new ConstantCallSite(serviceType, defaultValue); } if (callSite is null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(string.Format("Unable to resolve service for type '{0}' while attempting to activate '{1}'.", item.ParameterType, implementationType)); } return(null); } parameterCallSites.Add(callSite); } return(parameterCallSites.ToArray()); }
private ServiceCallSite TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot) { if (serviceType == descriptor.ServiceType) { ServiceCallSite callSite; var lifetime = new ResultCache(descriptor.Lifetime, serviceType, slot); if (descriptor.ImplementationInstance != null) { callSite = new ConstantCallSite(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory != null) { callSite = new FactoryCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationFactory); } else if (descriptor.ImplementationType != null) { callSite = CreateConstructorCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationType, callSiteChain); } else { throw new InvalidOperationException("Invalid service descriptor"); } return(callSite); } return(null); }
private IServiceCallSite[] PopulateCallSites( ServiceProvider provider, ISet <Type> callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new IServiceCallSite[parameters.Length]; for (var index = 0; index < parameters.Length; index++) { var callSite = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain); if (callSite == null && parameters[index].HasDefaultValue) { callSite = new ConstantCallSite(parameters[index].DefaultValue); } if (callSite == null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(Resources.FormatCannotResolveService( parameters[index].ParameterType, _descriptor.ImplementationType)); } return(null); } parameterCallSites[index] = callSite; } return(parameterCallSites); }
private ServiceCallSite[] CreateArgumentCallSites( Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new ServiceCallSite[parameters.Length]; for (int index = 0; index < parameters.Length; index++) { Type parameterType = parameters[index].ParameterType; ServiceCallSite callSite = GetCallSite(parameterType, callSiteChain); if (callSite == null && ParameterDefaultValue.TryGetDefaultValue(parameters[index], out object defaultValue)) { callSite = new ConstantCallSite(parameterType, defaultValue); } if (callSite == null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(SR.Format(SR.CannotResolveService, parameterType, implementationType)); } return(null); } parameterCallSites[index] = callSite; } return(parameterCallSites); }
protected override object VisitConstant(ConstantCallSite constantCallSite, ILEmitResolverBuilderContext argument) { AddConstant(argument, constantCallSite.DefaultValue); if (constantCallSite.ServiceType.IsValueType) { argument.Generator.Emit(OpCodes.Unbox_Any, constantCallSite.ServiceType); } return(null); }
private ServiceCallSite[] CreateArgumentCallSites( Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new ServiceCallSite[parameters.Length]; for (var index = 0; index < parameters.Length; index++) { // 依次递归调用获取指定参数的ServiceCallSite var callSite = GetCallSite(parameters[index].ParameterType, callSiteChain); if (callSite == null && ParameterDefaultValue.TryGetDefaultValue(parameters[index], out var defaultValue)) { // 如果获取参数的ServiceCallSite失败但是该参数具有默认值 // 则直接以默认值来创建ConstantCallSite对象 callSite = new ConstantCallSite(serviceType, defaultValue); } // 如果当前callSite还为空,则代表出现无法实例化的参数类型 // 如果允许抛出异常则抛出异常,如果不允许抛出异常则返回null if (callSite == null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(Resources.FormatCannotResolveService( parameters[index].ParameterType, implementationType)); } return(null); } parameterCallSites[index] = callSite; } return(parameterCallSites); }
/// <summary> /// 根据注册服务的方式进行实例化 /// </summary> /// <param name="descriptor"></param> /// <param name="serviceType"></param> /// <param name="callSiteChain"></param> /// <param name="slot"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> private ServiceCallSite TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, int slot) { //判断基类类型是否与ServiceDescriptor所持有的基类类型是否一致,如果不一致直接返回false if (serviceType == descriptor.ServiceType) { //根据当前注册的生命周期,基类类型和slot实例化一个ResultCache,ResultCache类型具有一个最后结果缓存的位置(相当于跟生命周期一致)和一个缓存Key ServiceCallSite callSite; var lifetime = new ResultCache(descriptor.Lifetime, serviceType, slot); //根据注册时所使用的方式来创建不同的ServiceCallSite,共具有三种ServiceCallSite子类 if (descriptor.ImplementationInstance != null) { //注册时直接根据对象进行实例化具体对象(Singleton生命周期独有) callSite = new ConstantCallSite(descriptor.ServiceType, descriptor.ImplementationInstance); } else if (descriptor.ImplementationFactory != null) { //FactoryCallSite 注册时根据一个工厂实例化对象 callSite = new FactoryCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationFactory); } else if (descriptor.ImplementationType != null) { //ConstructorCallSite 注册时根据具体实例类型进行实例化对象,如果注册类型是使用的派生类类型方式,则调用CreateConstructorCallSite来实例化一个ConstructorCallSite callSite = CreateConstructorCallSite(lifetime, descriptor.ServiceType, descriptor.ImplementationType, callSiteChain); } else { throw new InvalidOperationException("Invalid service descriptor"); } return(callSite); } return(null); }
protected override Expression VisitConstant(ConstantCallSite constantCallSite, object context) { return(Expression.Constant(constantCallSite.DefaultValue)); }
protected abstract TResult VisitConstant(ConstantCallSite constantCallSite, TArgument argument);
protected override Type VisitConstant(ConstantCallSite constantCallSite, CallSiteValidatorState state) => null;
protected override Expression VisitConstant(ConstantCallSite constantCallSite, ParameterExpression provider) { return(Expression.Constant(constantCallSite.DefaultValue)); }
protected override object VisitConstant(ConstantCallSite constantCallSite, ILEmitResolverBuilderContext argument) { AddConstant(argument, constantCallSite.DefaultValue); return(null); }
protected override object VisitConstant(ConstantCallSite constantCallSite, RuntimeResolverContext context) { return(constantCallSite.DefaultValue);//直接返回ConstantCallSite的值 }
private IServiceCallSite[] PopulateCallSites( ServiceProvider provider, ISet<Type> callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound) { var parameterCallSites = new IServiceCallSite[parameters.Length]; for (var index = 0; index < parameters.Length; index++) { var callSite = provider.GetServiceCallSite(parameters[index].ParameterType, callSiteChain); if (callSite == null && parameters[index].HasDefaultValue) { callSite = new ConstantCallSite(parameters[index].DefaultValue); } if (callSite == null) { if (throwIfCallSiteNotFound) { throw new InvalidOperationException(Resources.FormatCannotResolveService( parameters[index].ParameterType, _descriptor.ImplementationType)); } return null; } parameterCallSites[index] = callSite; } return parameterCallSites; }
protected override object VisitConstant(ConstantCallSite constantCallSite, ServiceProvider provider) { return(constantCallSite.DefaultValue); }