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 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); }
protected override object VisitFactory(FactoryCallSite factoryCallSite, ILEmitResolverBuilderContext argument) { if (argument.Factories == null) { argument.Factories = new List <Func <IServiceProvider, object> >(); } // this.Factories[i](ProviderScope) argument.Generator.Emit(OpCodes.Ldarg_0); argument.Generator.Emit(OpCodes.Ldfld, FactoriesField); argument.Generator.Emit(OpCodes.Ldc_I4, argument.Factories.Count); argument.Generator.Emit(OpCodes.Ldelem, typeof(Func <IServiceProvider, object>)); argument.Generator.Emit(OpCodes.Ldarg_1); argument.Generator.Emit(OpCodes.Call, ExpressionResolverBuilder.InvokeFactoryMethodInfo); argument.Factories.Add(factoryCallSite.Factory); return(null); }
/// <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 VisitFactory(FactoryCallSite factoryCallSite, object context) { return(Expression.Invoke(Expression.Constant(factoryCallSite.Factory), ScopeParameter)); }
protected abstract TResult VisitFactory(FactoryCallSite factoryCallSite, TArgument argument);
protected override Type VisitFactory(FactoryCallSite factoryCallSite, CallSiteValidatorState state) => null;
/// <summary> /// /// </summary> /// <param name="factoryCallSite"></param> /// <param name="context"></param> /// <returns></returns> protected override object VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) { //调用工厂方法进行实例化 return(factoryCallSite.Factory(context.Scope)); }
protected override Expression VisitFactory(FactoryCallSite factoryCallSite, ParameterExpression provider) { return(Expression.Invoke(Expression.Constant(factoryCallSite.Factory), provider)); }
protected override object VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider2 provider) { return(factoryCallSite.Factory(provider)); }
protected override object VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) { return(factoryCallSite.Factory(scope)); }