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 IServiceCallSite TryCreateOpenGeneric(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain) { if (serviceType.IsConstructedGenericType && serviceType.GetGenericTypeDefinition() == descriptor.ServiceType) { Debug.Assert(descriptor.ImplementationType != null, "descriptor.ImplementationType != null"); var closedType = descriptor.ImplementationType.MakeGenericType(serviceType.GenericTypeArguments); var constructorCallSite = CreateConstructorCallSite(serviceType, closedType, callSiteChain); var callSite = new InterceptionCallSite(_interceptingProxyFactory, constructorCallSite); return(ApplyLifetime(callSite, Tuple.Create(descriptor, serviceType), descriptor.Lifetime)); } return(null); }
protected override object VisitInterception(InterceptionCallSite interceptionCallSite, ServiceProvider2 provider) { if (interceptionCallSite.ServiceType.IsInterface) { var target = this.VisitCallSite(interceptionCallSite.TargetCallSite, provider); return(interceptionCallSite.ProxyFactory.Wrap(interceptionCallSite.ServiceType, target)); } if (interceptionCallSite.TargetCallSite is ConstructorCallSite || interceptionCallSite.TargetCallSite is CreateInstanceCallSite) { return(interceptionCallSite.ProxyFactory.Create(interceptionCallSite.ImplementationType ?? interceptionCallSite.ServiceType, provider, () => VisitCallSite(interceptionCallSite.TargetCallSite, provider))); } return(this.VisitCallSite(interceptionCallSite.TargetCallSite, provider)); }
protected override object VisitInterception(InterceptionCallSite interceptionCallSite, ServiceProviderEngineScope scope) { if (interceptionCallSite.ServiceType.IsInterface) { var target = VisitCallSite(interceptionCallSite.TargetCallSite, scope); return(interceptionCallSite.ProxyFactory.Wrap(interceptionCallSite.ServiceType, target)); } if (interceptionCallSite.CanIntercept && (interceptionCallSite.TargetCallSite is ConstructorCallSite || interceptionCallSite.TargetCallSite is CreateInstanceCallSite)) { return(interceptionCallSite.ProxyFactory.Create(interceptionCallSite.ImplementationType ?? interceptionCallSite.ServiceType, scope, () => VisitCallSite(interceptionCallSite.TargetCallSite, scope))); } return(VisitCallSite(interceptionCallSite.TargetCallSite, scope)); }
protected override Expression VisitInterception(InterceptionCallSite interceptionCallSite, CallSiteExpressionBuilderContext argument) { if (interceptionCallSite.ServiceType.IsInterface) { var type = Expression.Constant(interceptionCallSite.ServiceType); var target = VisitCallSite(interceptionCallSite.TargetCallSite, argument); var proxyFactory = Expression.Constant(interceptionCallSite.ProxyFactory, typeof(IInterceptingProxyFactory)); return(Expression.Call(proxyFactory, WrapMethodInfo, type, target)); } if (interceptionCallSite.CanIntercept && (interceptionCallSite.TargetCallSite is ConstructorCallSite || interceptionCallSite.TargetCallSite is CreateInstanceCallSite)) { var proxyFactory = Expression.Constant(interceptionCallSite.ProxyFactory, typeof(IInterceptingProxyFactory)); var type = Expression.Constant(interceptionCallSite.ImplementationType ?? interceptionCallSite.ServiceType); var targetAccessor = Expression.Lambda <Func <object> >(VisitCallSite(interceptionCallSite.TargetCallSite, argument)); return(Expression.Call(proxyFactory, CreateMethodInfo, type, argument.ScopeParameter, targetAccessor)); } return(VisitCallSite(interceptionCallSite.TargetCallSite, argument)); }
protected override Expression VisitInterception(InterceptionCallSite interceptionCallSite, ParameterExpression provider) { if (interceptionCallSite.ServiceType.IsInterface) { var instance = Expression.Constant(interceptionCallSite.ProxyFactory, typeof(IInterceptingProxyFactory)); var parameterOfServiceType = Expression.Constant(interceptionCallSite.ServiceType); var parameterOfTarget = this.VisitCallSite(interceptionCallSite.TargetCallSite, provider); return(Expression.Call(instance, WrapMethodInfo, parameterOfServiceType, parameterOfTarget)); } else if (interceptionCallSite.TargetCallSite is ConstructorCallSite || interceptionCallSite.TargetCallSite is CreateInstanceCallSite) { var instance = Expression.Constant(interceptionCallSite.ProxyFactory, typeof(IInterceptingProxyFactory)); var parameterOfServiceType = Expression.Constant(interceptionCallSite.ImplementationType ?? interceptionCallSite.ServiceType); var targetAccessor = Expression.Lambda <Func <object> >(this.VisitCallSite(interceptionCallSite.TargetCallSite, provider)); return(Expression.Call(instance, CreateMethodInfo, parameterOfServiceType, provider, targetAccessor)); } else { return(this.VisitCallSite(interceptionCallSite.TargetCallSite, provider)); } }
protected abstract TResult VisitInterception(InterceptionCallSite interceptionCallSite, TArgument argument);
protected override Type VisitInterception(InterceptionCallSite interceptionCallSite, CallSiteValidatorState argument) => null;