internal static object ParseResult()

in src/TestFramework.AdapterConsole/ConsoleHelper.cs [129:210]


        internal static object ParseResult(Type type, string result)
        {
            if (result == null)
            {
                // Empty string should be accepted by 'Parse', hence only check null reference.
                throw new ArgumentNullException("result");
            }

            // Convert to non-ref types
            if (type.IsByRef)
            {
                type = type.GetElementType();
            }

            MethodInfo mi;
            // Specially processing String type.
            if (type == typeof(String))
            {
                // Ingore String type.
                return result;
            }
            // Specially processing Enum type.
            else if (type.IsAssignableFrom(typeof(Enum)) || type.IsEnum)
            {
                try
                {
                    return Enum.Parse(type, result, true);
                }
                catch (ArgumentException)
                {
                    throw new FormatException();
                }
            }
            else
            {
                // Check if T has a 'Parse' method.
                try
                {
                    mi = type.GetMethod(
                        "Parse",
                        BindingFlags.Static | BindingFlags.Public,
                        null,
                        new Type[] { typeof(string) },
                        new ParameterModifier[0]
                        );
                }
                catch (AmbiguousMatchException e)
                {
                    throw new FormatException(
                        String.Format("More than one 'Parse' method is found in {0}.", type), e
                        );
                }
                if (mi == null)
                {
                    throw new FormatException(
                        String.Format(
                            "Can not parse the result, " +
                            "due to the type {0} doesn't contain a method 'public static {0} Parse (String)'.", type)
                        );
                }

                // Invoke and get the result.
                object res = null;
                try
                {
                    res = mi.Invoke(null, new object[] { result });
                }
                catch (TargetInvocationException e)
                {
                    if (e.InnerException != null && e.InnerException is FormatException)
                    {
                        throw e.InnerException;
                    }
                    else
                    {
                        throw;
                    }
                }

                return res;
            }
        }