Example #1
0
        /// <summary>
        /// Prepare the invoke parameters. Adds default values if a parameter is missing.
        /// </summary>
        /// <param name="originalParameters">Original parameters.</param>
        /// <param name="executor">Handler executor.</param>
        /// <returns>Handler parameters.</returns>
        private object[] PrepareParameters(object[] originalParameters, HandlerExecutor executor)
        {
            if (!executor.MethodParameters.Any())
            {
                return(null);
            }

            var parameters = new object[executor.MethodParameters.Count()];

            for (var i = 0; i < parameters.Length; i++)
            {
                ParameterInfo methodParameterInfo = executor.MethodParameters.ElementAt(i);

                if (i < originalParameters.Length)
                {
                    Type originalObjectType = originalParameters[i]?.GetType();

                    if (!methodParameterInfo.ParameterType.IsAssignableFrom(originalObjectType))
                    {
                        object transformedParameter = this.parameterTransformer.Transform(originalParameters[i], methodParameterInfo.ParameterType.GetTypeInfo());

                        parameters[i] = transformedParameter ?? executor.GetDefaultValueForParameter(i);
                    }
                    else
                    {
                        parameters[i] = originalParameters[i];
                    }
                }
                else
                {
                    parameters[i] = executor.GetDefaultValueForParameter(i);
                }
            }

            return(parameters);
        }
 /// <summary>
 /// Creates a new <see cref="HandlerActionInvokerCacheEntry"/> instance.
 /// </summary>
 /// <param name="handlerType">Target handler type.</param>
 /// <param name="handlerFactory">Handler factory creator function.</param>
 /// <param name="handlerReleaser">Handler releaser action.</param>
 /// <param name="handlerExecutor">Handler action executor.</param>
 internal HandlerActionInvokerCacheEntry(Type handlerType, Func <Type, object> handlerFactory, Action <object> handlerReleaser, HandlerExecutor handlerExecutor)
 {
     this.HandlerType     = handlerType;
     this.HandlerFactory  = handlerFactory;
     this.HandlerReleaser = handlerReleaser;
     this.HandlerExecutor = handlerExecutor;
 }