/// <summary> /// Checks if an <see cref="IConstructorArgument"/> with another type than <see cref="FuncConstructorArgument"/> applies to the target. /// </summary> /// <param name="context">The context.</param> /// <param name="target">The target.</param> /// <returns>True if an <see cref="IConstructorArgument"/> with another type than <see cref="FuncConstructorArgument"/> applies to the target.</returns> private static bool CheckOtherConstructorArgumentApplies(IContext context, ParameterTarget target) { return(context .Parameters .OfType <IConstructorArgument>() .Any(p => !(p is FuncConstructorArgument) && p.AppliesToTarget(context, target))); }
public StandardProviderBenchmark() { var ninjectSettings = new NinjectSettings { // Disable to reduce memory pressure ActivationCacheDisabled = true, LoadExtensions = false }; var kernelConfigurationWithoutBindings = new KernelConfiguration(ninjectSettings); #region FromConstructorArguments _contextWithConstructorArguments = CreateContext(kernelConfigurationWithoutBindings, kernelConfigurationWithoutBindings.BuildReadOnlyKernel(), new List <IParameter> { new ConstructorArgument("location", "Biutiful"), new PropertyValue("warrior", "cutter"), new ConstructorArgument("warrior", new Monk()), new ConstructorArgument("weapon", new Dagger()), }, typeof(NinjaBarracks), ninjectSettings); _contextWithConstructorArguments.Plan = kernelConfigurationWithoutBindings.Components.Get <IPlanner>().GetPlan(typeof(NinjaBarracks)); #endregion FromConstructorArguments #region FromBindings var kernelConfigurationWithBindings = new KernelConfiguration(ninjectSettings); kernelConfigurationWithBindings.Bind <IWarrior>().To <Monk>().InSingletonScope(); kernelConfigurationWithBindings.Bind <IWeapon>().To <Dagger>().InSingletonScope(); _contextWithoutConstructorArguments = CreateContext(kernelConfigurationWithBindings, kernelConfigurationWithBindings.BuildReadOnlyKernel(), new List <IParameter>(), typeof(NinjaBarracks), ninjectSettings); _contextWithoutConstructorArguments.Plan = kernelConfigurationWithBindings.Components.Get <IPlanner>().GetPlan(typeof(NinjaBarracks)); #endregion FromBindings #region FromDefaultConstructor _contextWithDefaultConstructor = CreateContext(kernelConfigurationWithBindings, kernelConfigurationWithBindings.BuildReadOnlyKernel(), new List <IParameter>(), typeof(Dagger), ninjectSettings); _contextWithDefaultConstructor.Plan = kernelConfigurationWithBindings.Components.Get <IPlanner>().GetPlan(typeof(Dagger)); #endregion FromDefaultConstructor _warriorParameterTarget = CreateWarriorParameterTarget(); _standardProvider = new StandardProvider(typeof(StandardProviderBenchmark), kernelConfigurationWithoutBindings.Components.Get <IPlanner>(), kernelConfigurationWithoutBindings.Components.Get <IConstructorScorer>()); }
/// <summary> /// Creates targets for the parameters of the method. /// </summary> /// <param name="method">The method.</param> /// <returns>The targets for the method's parameters.</returns> protected virtual ITarget[] CreateTargetsFromParameters(TMethod method) { var parameters = method.GetParameters(); if (parameters.Length == 0) { return(Array.Empty <ITarget>()); } var targets = new ITarget[parameters.Length]; for (var i = 0; i < parameters.Length; i++) { targets[i] = new ParameterTarget(method, parameters[i]); } return(targets); }
private static string GetFormatterName(ParameterModel parameter, ParameterTarget target) { var goParameterAttribute = parameter.Attributes.OfType <GoParameterAttribute>().SingleOrDefault(); // 如果 attribute name有效,则无条件使用 attribute 提供的 name if (goParameterAttribute?.Name != null) { return(goParameterAttribute.Name); } // 目标是 path 则使用参数名称 if (target == ParameterTarget.Path) { return(parameter.ParameterName); } // 如果对应目标只有一个参数则name为null,否则使用原参数名称 return(parameter.Method.Parameters.GroupBy(i => i.Target).Count() == 1 ? null : parameter.ParameterName); }
/// <summary> /// Gets the position of the parameter specified by the target relative to the other parameters of the same /// type of the method containing the target. Parameters that apply to other ConstructorArguments are ignored. /// </summary> /// <param name="context">The current context.</param> /// <param name="target">The target for which the position is calculated.</param> /// <returns> /// -1 if the target is not found of the parameter applies to another constructor argument. /// Otherwise the position of the target relative to the other parameters of the method that have the same type and /// do not apply to another <see cref="ConstructorArgument"/>. /// </returns> public int GetTargetPosition(IContext context, ITarget target) { int targetPosition = 0; var constructorInfo = (ConstructorInfo)target.Member; foreach (var parameter in constructorInfo.GetParametersOfType(target.Type)) { var newTarget = new ParameterTarget(constructorInfo, parameter); if (!CheckOtherConstructorArgumentApplies(context, newTarget)) { if (parameter.Name == target.Name) { return(targetPosition); } targetPosition++; } } return(-1); }
public GoParameterAttribute(string name, ParameterTarget target) { Name = name; Target = target; }
public GoParameterAttribute(ParameterTarget target) : this(null, target) { }
/// <summary> /// Gets the position of the parameter specified by the target relative to the other parameters of the same /// type of the method containing the target. Parameters that apply to other ConstructorArguments are ignored. /// </summary> /// <param name="context">The current context.</param> /// <param name="target">The target for which the position is calculated.</param> /// <returns> /// -1 if the target is not found of the parameter applies to another constructor argument. /// Otherwise the position of the target relative to the other parameters of the method that have the same type and /// do not apply to another <see cref="ConstructorArgument"/>. /// </returns> public int GetTargetPosition(IContext context, ITarget target) { int targetPosition = 0; var constructorInfo = (ConstructorInfo)target.Member; foreach (var parameter in constructorInfo.GetParametersOfType(target.Type)) { var newTarget = new ParameterTarget(constructorInfo, parameter); if (!CheckOtherConstructorArgumentApplies(context, newTarget)) { if (parameter.Name == target.Name) { return targetPosition; } targetPosition++; } } return -1; }
/// <summary> /// Checks if an <see cref="IConstructorArgument"/> with another type than <see cref="FuncConstructorArgument"/> applies to the target. /// </summary> /// <param name="context">The context.</param> /// <param name="target">The target.</param> /// <returns>True if an <see cref="IConstructorArgument"/> with another type than <see cref="FuncConstructorArgument"/> applies to the target.</returns> private static bool CheckOtherConstructorArgumentApplies(IContext context, ParameterTarget target) { return context .Parameters .OfType<IConstructorArgument>() .Any(p => !(p is FuncConstructorArgument) && p.AppliesToTarget(context, target)); }