예제 #1
0
 void Registrate(Type type, CommandAttribute attribute)
 {
     ReflectionTools.ThrowIfItIsNotValidCommand(type);
     RegistrateUnsafe(type, attribute);
 }
예제 #2
0
파일: ParseTools.cs 프로젝트: tmteam/Gin
        public static object Convert(string value, Type nonNullType, string alias)
        {
            if (ReflectionTools.IsNullable(nonNullType))
            {
                throw new ArgumentException();
            }

            if (value.StartsWith("\"") && value.EndsWith("\""))
            {
                value = value.Remove(value.Length - 1).Substring(1);
            }

            if (nonNullType == typeof(bool))
            {
                value = value.ToLower();
                if (trueStrings.Contains(value))
                {
                    return(true);
                }
                if (falseStrings.Contains(value))
                {
                    return(false);
                }
            }
            else if (nonNullType.IsEnum)
            {
                try
                {
                    var val = int.Parse(value);
                    return(System.Convert.ChangeType(val, nonNullType));
                }
                catch
                {
                    //don't worry - it's ok
                }
                try {
                    //Give them another chance
                    return(Enum.Parse(nonNullType, value, true));
                } catch {
                    //Now let them fall down...
                }
            }
            else
            {
                try
                {
                    if (nonNullType == typeof(string))
                    {
                        return(value);
                    }
                    if (nonNullType == typeof(DateTime))
                    {
                        return(new DateTimeValue(value).ToDateTime());
                    }
                    if (nonNullType == typeof(TimeSpan))
                    {
                        return(new DateTimeValue(value).ToTimeSpan());
                    }
                    if (nonNullType == typeof(double) ||
                        nonNullType == typeof(float) ||
                        nonNullType == typeof(decimal))
                    {
                        var format = CultureInfo.CurrentCulture.NumberFormat;
                        if (format.NumberDecimalSeparator == ".")
                        {
                            value = value.Replace(',', '.');
                        }
                        else
                        {
                            value = value.Replace('.', ',');
                        }
                    }
                    return(System.Convert.ChangeType(value, nonNullType));
                }
                catch
                {
                    //Just let them fall...
                }
            }
            throw new InvalidArgumentException(nonNullType, value, alias);
        }