Пример #1
0
        private static MethodBindResult BindMethod(List <MethodInfo> methods, string methodName, CommandLineArguments arguments)
        {
            if (methods == null)
            {
                throw new ArgumentNullException("methods");
            }
            if (methodName == null)
            {
                throw new ArgumentNullException("methodName");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            var failedMethods = new Dictionary <MethodInfo, ParameterBindResult[]>();

            foreach (var method in methods)
            {
                if (string.Equals(method.Name, methodName, StringComparison.OrdinalIgnoreCase) == false ||
                    !method.IsStatic ||
                    method.ReturnType != typeof(int))
                {
                    continue;
                }

                var parameters          = method.GetParameters();
                var parameterBindings   = new ParameterBindResult[parameters.Length];
                var isSuccessfulBinding = true;
                foreach (var parameter in parameters)
                {
                    var bindResult = BindParameter(parameter, arguments);
                    parameterBindings[parameter.Position] = bindResult;
                    isSuccessfulBinding = isSuccessfulBinding && bindResult.IsSuccess;
                }

                if (isSuccessfulBinding == false)
                {
                    failedMethods.Add(method, parameterBindings);
                    continue;
                }

                var methodArguments = new object[parameters.Length];
                for (var i = 0; i < parameters.Length; i++)
                {
                    methodArguments[i] = parameterBindings[i].Value;
                }

                return(new MethodBindResult(method, methodArguments));
            }

            return(new MethodBindResult(methodName, failedMethods));
        }