Exemplo n.º 1
0
        /// <summary>
        /// Retrieves the object parameters for a method
        /// </summary>
        /// <param name="method">Method we are retrieving the parameters for</param>
        /// <param name="input">Console input to parse</param>
        /// <returns>object[] of the parameters</returns>
        private object[] getParameters(MethodInfo method, string input)
        {
            object[] parameters = null;
            // determine if we have parameters
            if (input.Contains('('))
            {
                // we do so figure out what they are now
                List <string>   parms      = new List <string>();
                ParameterInfo[] paramInfos = method.GetParameters();
                if (paramInfos != null && paramInfos.Length >= 1)
                {
                    // Thanks to WolfgangKluge at MSDN for this regex pattern (http://social.msdn.microsoft.com/Forums/en/regexp/thread/bfa96ae8-6df6-4308-811e-29fc25156c4d)
                    string          pattern         = @"^(?<member>(?>[^(]+))\(
					(?:
						(?<parameter>
							(?:
								(?>[^,()""']+)|
								""(?>[^\\""]+|\\"")*""|
								@""(?>[^""]+|"""")*""|
								'(?:[^']|\\')'|
								\(
									(?:
										(?>[^()""']+)|
										""(?>[^\\""]+|\\"")*""|
										@""(?>[^""]+|"""")*""|
										'(?:[^']|\\')'|
										(?<nest>\()|
										(?<-nest>\))
									)*
									(?(nest)(?!))
								\)
							)+
						)
						\s*
						(?(?=,),\s*|(?=\)))
					)+
					\)
					"                    ;
                    MatchCollection matchCollection = Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace);
                    if (matchCollection == null || matchCollection.Count == 0)
                    {
                        throw new ScriptException("Malformed method call, ensure every opening brace has a closing brace");
                    }
                    GroupCollection   groups   = null;
                    CaptureCollection captures = null;
                    bool foundMethodStatement;
                    foreach (Match match in matchCollection)
                    {
                        groups = match.Groups;
                        if (groups != null)
                        {
                            foundMethodStatement = false;
                            foreach (Group group in groups)
                            {
                                captures = group.Captures;
                                if (captures != null)
                                {
                                    foreach (Capture capture in captures)
                                    {
                                        if (foundMethodStatement)
                                        {
                                            parms.Add(capture.Value);
                                        }
                                        else
                                        {
                                            if (capture.Value.EndsWith(method.Name))
                                            {
                                                foundMethodStatement = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    parameters = new object[parms.Count];
                    string value = null;
                    for (int i = 0; i < parms.Count; i++)
                    {
                        Type type = paramInfos[i].ParameterType;
                        value         = parms[i];
                        value         = ScriptUtils.scrubStringParameter(value, type);
                        parameters[i] = ObjectTranslator.translate(type, value);
                    }
                }
            }
            return(parameters);
        }