NAnt.Core.Util.CommandLineArgument.ParseValue C# (CSharp) Method

ParseValue() private method

private ParseValue ( Type type, string stringData ) : object
type System.Type
stringData string
return object
        private object ParseValue(Type type, string stringData)
        {
            // null is only valid for bool variables
            // empty string is never valid
            if ((stringData != null || type == typeof(bool)) && (stringData == null || stringData.Length > 0)) {
                try {
                    if (type == typeof(string)) {
                        return stringData;
                    } else if (type == typeof(bool)) {
                        if (stringData == null || stringData == "+") {
                            return true;
                        } else if (stringData == "-") {
                            return false;
                        }
                    } else if (IsNameValueCollectionType(type)) {
                        Match match = Regex.Match(stringData, @"(\w+[^=]*)=(\w*.*)");
                        if (match.Success) {
                            string name = match.Groups[1].Value;
                            string value = match.Groups[2].Value;

                            if (Unique && _valuePairs.Get(name) != null) {
                                // we always assume we're dealing with properties
                                // here to make the message more clear
                                throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture,
                                    ResourceUtils.GetString("NA1174"),
                                    name, LongName));
                            }
                            _valuePairs.Add(name, value);
                            return _valuePairs;
                        } else {
                            throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture,
                                ResourceUtils.GetString("NA1170"),
                                stringData, LongName), new ArgumentException(
                                "Expected name/value pair (<name>=<value>)."));
                        }
                    } else {
                        if (type.IsEnum) {
                            try {
                                return Enum.Parse(type, stringData, true);
                            } catch(ArgumentException ex) {
                                string message = "Invalid value {0} for command-line argument '-{1}'. Valid values are: ";
                                foreach (object value in Enum.GetValues(type)) {
                                    message += value.ToString() + ", ";
                                }
                                // strip last ,
                                message = message.Substring(0, message.Length - 2) + ".";
                                throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture,
                                    message, stringData, LongName), ex);
                            }
                        } else {
                            // Make a guess that the there's a public static Parse method on the type of the property
                            // that will take an argument of type string to convert the string to the type
                            // required by the property.
                            System.Reflection.MethodInfo parseMethod = type.GetMethod(
                                "Parse", BindingFlags.Public | BindingFlags.Static,
                                null, CallingConventions.Standard, new Type[] {typeof(string)},
                                null);

                            if (parseMethod != null) {
                                // Call the Parse method
                                return parseMethod.Invoke(null, BindingFlags.Default,
                                    null, new object[] {stringData}, CultureInfo.InvariantCulture);
                            } else if (type.IsClass) {
                                // Search for a constructor that takes a string argument
                                ConstructorInfo stringArgumentConstructor =
                                    type.GetConstructor(new Type[] {typeof(string)});

                                if (stringArgumentConstructor != null) {
                                    return stringArgumentConstructor.Invoke(
                                        BindingFlags.Default, null, new object[] {stringData},
                                        CultureInfo.InvariantCulture);
                                }
                            }
                        }
                    }
                } catch (CommandLineArgumentException) {
                    throw;
                } catch (Exception ex) {
                    throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1170"),
                        stringData, LongName), ex);
                }
            }

            throw new CommandLineArgumentException(string.Format(CultureInfo.InvariantCulture,
                ResourceUtils.GetString("NA1170"), stringData,
                LongName));
        }