示例#1
0
        private static MethodBindResult BindMethod(Type type, string defaultMethodName, CommandLineArguments arguments)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            if (string.IsNullOrEmpty(defaultMethodName))
            {
                defaultMethodName = null;
            }

            var allMethods = type.GetTypeInfo().GetAllMethods().Where(IsValidCommandSignature).ToList();

            allMethods.Sort((x, y) => y.GetParameters().Length.CompareTo(x.GetParameters().Length));

            var methodName      = default(string);
            var bestMatchResult = default(MethodBindResult);

            if (arguments.ContainsKey("0"))
            {
                var newArguments = new CommandLineArguments(arguments);
                methodName = TypeConvert.ToString(newArguments["0"]);
                newArguments.RemoveAt(0);
                var result = BindMethod(allMethods, methodName, newArguments);
                if (result.IsSuccess)
                {
                    return(result);
                }

                bestMatchResult = result;
            }

            if (string.IsNullOrEmpty(defaultMethodName) == false)
            {
                methodName = defaultMethodName;
                var result = BindMethod(allMethods, defaultMethodName, arguments);
                if (result.IsSuccess)
                {
                    return(result);
                }

                bestMatchResult = bestMatchResult ?? result;
            }

            if (string.IsNullOrEmpty(methodName))
            {
                methodName = UnknownMethodName;
            }

            bestMatchResult = bestMatchResult ?? new MethodBindResult(methodName, MethodBindResult.EmptyFailedMethodBindings);

            return(bestMatchResult);
        }
示例#2
0
        private static MethodBindResult BindMethod(Type type, string defaultMethodName, CommandLineArguments arguments)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            var methodName = defaultMethodName;

            if (arguments.ContainsKey("0"))
            {
                arguments  = new CommandLineArguments(arguments);
                methodName = TypeConvert.ToString(arguments["0"]);
                arguments.RemoveAt(0);
            }

            var candidate  = default(MethodInfo);
            var allMethods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

            Array.Sort(allMethods, (x, y) => y.GetParameters().Length.CompareTo(x.GetParameters().Length));
            foreach (var method in allMethods)
            {
                if (string.Equals(method.Name, methodName, StringComparison.OrdinalIgnoreCase) == false || !method.IsStatic || method.ReturnType != typeof(int) || IsHidden(method))
                {
                    continue;
                }

                candidate = method;
                var parameters = method.GetParameters();
                var methodArgs = new object[parameters.Length];
                foreach (var parameter in parameters)
                {
                    var value = default(object);
                    if (TryBindParameter(parameter, arguments, out value) == false)
                    {
                        goto nextMethod;
                    }
                    methodArgs[parameter.Position] = value;
                }

                return(new MethodBindResult(method, methodArgs));

nextMethod:
                ;
            }

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