/// <summary> /// Called during the chain of responsibility for a build operation. The /// PreBuildUp method is called when the chain is being executed in the /// forward direction. /// </summary> /// <param name="context">Context of the build operation.</param> // FxCop suppression: Validation is done by Guard class public override void PreBuildUp(IBuilderContext context) { DynamicBuildPlanGenerationContext ilContext = (DynamicBuildPlanGenerationContext)(context.Existing); IMethodSelectorPolicy selector = context.Policies.Get <IMethodSelectorPolicy>(context.BuildKey); foreach (SelectedMethod method in selector.SelectMethods(context)) { GuardMethodIsNotOpenGeneric(method.Method); GuardMethodHasNoOutParams(method.Method); GuardMethodHasNoRefParams(method.Method); ParameterInfo[] parameters = method.Method.GetParameters(); ilContext.EmitLoadExisting(); int i = 0; foreach (string key in method.GetParameterKeys()) { ilContext.EmitResolveDependency(parameters[i].ParameterType, key); ++i; } ilContext.IL.EmitCall(OpCodes.Callvirt, method.Method, null); if (method.Method.ReturnType != typeof(void)) { ilContext.IL.Emit(OpCodes.Pop); } } }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PreBuildUp method is called when the chain is being executed in the /// forward direction. /// </summary> /// <param name="context">Context of the build operation.</param> // FxCop suppression: Validation is done by Guard class public override void PreBuildUp(IBuilderContext context) { DynamicBuildPlanGenerationContext ilContext = (DynamicBuildPlanGenerationContext)(context.Existing); IMethodSelectorPolicy selector = context.Policies.Get <IMethodSelectorPolicy>(context.BuildKey); LocalBuilder currentMethodName = ilContext.IL.DeclareLocal(typeof(string)); LocalBuilder currentParameterName = ilContext.IL.DeclareLocal(typeof(string)); ilContext.IL.BeginExceptionBlock(); foreach (SelectedMethod method in selector.SelectMethods(context)) { ilContext.IL.Emit(OpCodes.Ldstr, GetMethodSignature(method.Method)); ilContext.IL.Emit(OpCodes.Stloc, currentMethodName); ilContext.IL.Emit(OpCodes.Ldnull); ilContext.IL.Emit(OpCodes.Stloc, currentParameterName); GuardMethodIsNotOpenGeneric(method.Method); GuardMethodHasNoOutParams(method.Method); GuardMethodHasNoRefParams(method.Method); ParameterInfo[] parameters = method.Method.GetParameters(); ilContext.EmitLoadExisting(); int i = 0; foreach (string key in method.GetParameterKeys()) { ilContext.IL.Emit(OpCodes.Ldstr, parameters[i].Name); ilContext.IL.Emit(OpCodes.Stloc, currentParameterName); ilContext.EmitResolveDependency(parameters[i].ParameterType, key); ++i; } ilContext.IL.Emit(OpCodes.Ldnull); ilContext.IL.Emit(OpCodes.Stloc, currentParameterName); ilContext.IL.EmitCall(OpCodes.Callvirt, method.Method, null); if (method.Method.ReturnType != typeof(void)) { ilContext.IL.Emit(OpCodes.Pop); } } ilContext.IL.BeginCatchBlock(typeof(Exception)); Label parameterResolveFailed = ilContext.IL.DefineLabel(); ilContext.IL.Emit(OpCodes.Ldloc, currentParameterName); ilContext.IL.Emit(OpCodes.Brtrue, parameterResolveFailed); // Failure was in the method call. ilContext.IL.Emit(OpCodes.Rethrow); ilContext.IL.MarkLabel(parameterResolveFailed); ilContext.IL.Emit(OpCodes.Ldloc, currentMethodName); ilContext.IL.Emit(OpCodes.Ldloc, currentParameterName); ilContext.IL.EmitCall(OpCodes.Call, throwOnParameterResolveFailed, null); ilContext.IL.EndExceptionBlock(); }
private static SpecifiedMethodsSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToCreate, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToCreate, name); IMethodSelectorPolicy selector = policies.GetNoDefault <IMethodSelectorPolicy>(key, false); if (selector == null || !(selector is SpecifiedMethodsSelectorPolicy)) { selector = new SpecifiedMethodsSelectorPolicy(); policies.Set <IMethodSelectorPolicy>(selector, key); } return((SpecifiedMethodsSelectorPolicy)selector); }
/// <summary> /// Called during the chain of responsibility for a build operation. The /// PreBuildUp method is called when the chain is being executed in the /// forward direction. /// </summary> /// <param name="context">Context of the build operation.</param> // FxCop suppression: Validation is done by Guard class public override void PreBuildUp(IBuilderContext context) { DynamicBuildPlanGenerationContext ilContext = (DynamicBuildPlanGenerationContext)(context.Existing); IMethodSelectorPolicy selector = context.Policies.Get <IMethodSelectorPolicy>(context.BuildKey); foreach (SelectedMethod method in selector.SelectMethods(context)) { string signatureString = GetMethodSignature(method.Method); GuardMethodIsNotOpenGeneric(method.Method); GuardMethodHasNoOutParams(method.Method); GuardMethodHasNoRefParams(method.Method); ParameterInfo[] parameters = method.Method.GetParameters(); ilContext.EmitLoadExisting(); int i = 0; foreach (string key in method.GetParameterKeys()) { // Set the current operation ilContext.IL.Emit(OpCodes.Ldstr, parameters[i].Name); ilContext.IL.Emit(OpCodes.Ldstr, signatureString); ilContext.EmitLoadContext(); ilContext.IL.EmitCall(OpCodes.Call, setCurrentOperationToResolvingParameter, null); // Resolve the parameter ilContext.EmitResolveDependency(parameters[i].ParameterType, key); ++i; } // Set the current operation ilContext.IL.Emit(OpCodes.Ldstr, signatureString); ilContext.EmitLoadContext(); ilContext.IL.EmitCall(OpCodes.Call, setCurrentOperationToInvokingMethod, null); // Invoke the injection method ilContext.IL.EmitCall(OpCodes.Callvirt, method.Method, null); if (method.Method.ReturnType != typeof(void)) { ilContext.IL.Emit(OpCodes.Pop); } } // Clear the current operation ilContext.EmitClearCurrentOperation(); }
private static void ResolveMethodParameters(IBuilderContext context, IMethodSelectorPolicy methodSelector, IPolicyList resolverPolicyDestination, Dictionary <Tuple <string, string>, IList <StepScopeDependency> > methodParameters) { foreach (var method in methodSelector.SelectMethods(context, resolverPolicyDestination)) { var parameterResolvers = method.GetParameterResolvers(); for (var i = 0; i < parameterResolvers.Length; i++) { var namedTypeResolver = parameterResolvers[i] as NamedTypeDependencyResolverPolicy; if (namedTypeResolver != null) { var type = namedTypeResolver.Type; var name = namedTypeResolver.Name; if (StepScopeSynchronization.IsStepScope(type, name)) { var parameter = method.Method.GetParameters()[i]; var key = new Tuple <string, string>(GetSignature(method.Method), parameter.Name); AddMethodParameterDependency(methodParameters, key, new StepScopeDependency(type, name)); } } } } }
private static void ResolveMethodParameters(IBuilderContext context, IMethodSelectorPolicy methodSelector, IPolicyList resolverPolicyDestination, Dictionary<Tuple<string, string>, IList<StepScopeDependency>> methodParameters) { foreach (var method in methodSelector.SelectMethods(context, resolverPolicyDestination)) { var parameterResolvers = method.GetParameterResolvers(); for (var i = 0; i < parameterResolvers.Length; i++) { var namedTypeResolver = parameterResolvers[i] as NamedTypeDependencyResolverPolicy; if (namedTypeResolver != null) { var type = namedTypeResolver.Type; var name = namedTypeResolver.Name; if (StepScopeSynchronization.IsStepScope(type, name)) { var parameter = method.Method.GetParameters()[i]; var key = new Tuple<string, string>(GetSignature(method.Method), parameter.Name); AddMethodParameterDependency(methodParameters, key, new StepScopeDependency(type, name)); } } } } }