Ejemplo n.º 1
0
        internal static GraphCommand FromString(string content)
        {
            if (String.IsNullOrEmpty(content))
            {
                throw new ArgumentException("Invalid argument", "content");
            }

            content = content.Trim();
            if (String.IsNullOrEmpty(content))
            {
                throw new ArgumentException("Invalid argument", "content");
            }

            char[]   delimiter = new char[] { textDelimiter };
            string[] inputs    = content.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            if (null == inputs || (inputs.Length <= 0))
            {
                return(null);
            }

            Type         enumType    = typeof(GraphCommand.Name);
            object       commandName = Enum.Parse(enumType, inputs[0]);
            GraphCommand command     = new GraphCommand((Name)commandName);

            int inputIndex = 0;

            foreach (string input in inputs)
            {
                // Skip past the command name.
                if (inputIndex++ < 1)
                {
                    continue;
                }

                if (String.IsNullOrEmpty(input))
                {
                    throw new ArgumentException("Invalid argument", "content");
                }
                if ((input.Length < 2) || (input[1] != ':'))
                {
                    throw new ArgumentException("Invalid argument", "content");
                }

                int    intValue    = 0;
                bool   boolValue   = false;
                double doubleValue = 0.0;

                string value = input.Substring(2); // Skip past the "x:" portion.

                switch (input[0])
                {
                case 'b':
                    if (!Boolean.TryParse(value, out boolValue))
                    {
                        throw new InvalidCastException("Invalid 'bool' value");
                    }
                    command.AppendArgument(boolValue);
                    break;

                case 'd':
                    if (!Double.TryParse(value, out doubleValue))
                    {
                        throw new InvalidCastException("Invalid 'double' value");
                    }
                    command.AppendArgument(doubleValue);
                    break;

                case 'e':
                    int commaIndex = value.IndexOf(',');
                    if (-1 == commaIndex)
                    {
                        throw new ArgumentException("Invalid argument", "content");
                    }

                    string      fullTypeName = value.Substring(0, commaIndex);
                    string      enumValue    = value.Substring(commaIndex + 1); // Skip ','.
                    System.Type type         = GraphCommand.GetEnumType(fullTypeName);
                    command.AppendArgument(Enum.Parse(type, enumValue));
                    break;

                case 'i':
                    if (!Int32.TryParse(value, out intValue))
                    {
                        throw new InvalidCastException("Invalid 'int' value");
                    }
                    command.AppendArgument(intValue);
                    break;

                case 's':
                    string transformed = value.Replace("\\n", "\n");
                    command.AppendArgument(transformed);
                    break;

                case 'u':
                    if (value.StartsWith("0x") || value.StartsWith("0X"))
                    {
                        value = value.Substring(2);
                    }

                    uint unsignedValue = UInt32.Parse(value, NumberStyles.HexNumber);
                    command.AppendArgument(unsignedValue);
                    break;
                }
            }

            return(command);
        }