Beispiel #1
0
        internal static bool IsSimpleType(Type type)
        {
            var isListOfSimpleThings = false;

            if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
            {
                if (type.IsGenericType)
                {
                    var firstGenericParameter = type.GetGenericArguments().First();
                    var isResolveableType     = TypeResolver.IsResolvableType(firstGenericParameter);
                    isListOfSimpleThings = (firstGenericParameter.IsPrimitive || firstGenericParameter.IsEnum || firstGenericParameter == typeof(string) || isResolveableType);
                }
            }

            return(type.IsPrimitive || type.IsEnum || type == typeof(string) || isListOfSimpleThings);
        }
Beispiel #2
0
        internal static Parameter Create(ParameterInfo para, ParameterAttribute paraConfig)
        {
            if (IsSimpleType(para.ParameterType))
            {
                return(new SimpleParameter(para, paraConfig));
            }
            else if (TypeResolver.IsResolvableType(para.ParameterType))
            {
                return(new ResolvedParameter(para, paraConfig));
            }
            else
            {
                if (paraConfig != null)
                {
                    throw new ModelParmeterHasParameterAttributeException(para);
                }

                return(new ModelParameter(para));
            }
        }
Beispiel #3
0
        private List <ParameterAttribute> getMethodParameterInfo(MethodInfo method, bool enumerateModelProperties = false)
        {
            var paraConfigList = method.GetCustomAttributes <ParameterAttribute>();

            List <ParameterAttribute> pi = new List <ParameterAttribute>();

            foreach (var para in method.GetParameters())
            {
                if (Parameter.IsSimpleType(para.ParameterType) || TypeResolver.IsResolvableType(para.ParameterType))
                {
                    var paraConfig = paraConfigList.SingleOrDefault(x => x.ShortName.ToString().Equals(para.Name, StringComparison.CurrentCultureIgnoreCase) || string.Equals(x.LongName, para.Name, StringComparison.CurrentCultureIgnoreCase));
                    if (paraConfig == null)
                    {
                        if (para.Name.Length == 1)
                        {
                            paraConfig = new ParameterAttribute {
                                ShortName = (char)para.Name.First()
                            }
                        }
                    }
                    ;
                    else
                    {
                        paraConfig = new ParameterAttribute {
                            LongName = para.Name
                        }
                    };

                    if (para.HasDefaultValue)
                    {
                        paraConfig.IsRequired = false;
                        paraConfig.Default    = para.DefaultValue;
                    }

                    paraConfig.isSimpleType = Parameter.IsSimpleType(para.ParameterType);
                    paraConfig.Type         = para.ParameterType;

                    pi.Add(paraConfig);
                }
Beispiel #4
0
        private object getTypedParameterValue(ParameterAttribute para, CommandLineParameterInfo pi)
        {
            if (para.Type == typeof(bool))
            {
                return(pi.Exists);
            }

            bool isList = typeof(IEnumerable).IsAssignableFrom(para.Type) && para.Type != typeof(string);

            if (pi.HasValue)
            {
                var value = pi.Value;

                if (para.Type.IsEnum)
                {
                    if (Enum.TryParse(para.Type, value, ignoreCase: true, out object res))
                    {
                        return(res);
                    }
                    else
                    {
                        throw new InvalidParameterException(para);
                    }
                }

                if (TypeResolver.IsResolvableType(para.Type))
                {
                    return(TypeResolver.Resolve(value, para.Type));
                }

                if (isList)
                {
                    var separator = " ";
                    if (!string.IsNullOrWhiteSpace(para.ListSeparator))
                    {
                        separator = para.ListSeparator;
                    }

                    var splitValue = value.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);

                    var underlyingType      = para.Type.GetGenericArguments().FirstOrDefault();
                    var constructedListType = typeof(List <>).MakeGenericType(underlyingType);
                    var constructedList     = (IList)Activator.CreateInstance(constructedListType);

                    try
                    {
                        if (Parameter.IsSimpleType(underlyingType))
                        {
                            splitValue.ToList().ForEach(x => constructedList.Add(Convert.ChangeType(x, underlyingType)));
                        }
                        else if (TypeResolver.IsResolvableType(underlyingType))
                        {
                            splitValue.ToList().ForEach(x => constructedList.Add(TypeResolver.Resolve(x, underlyingType)));
                        }
                    }
                    catch (FormatException)
                    {
                        throw new InvalidParameterException(para);
                    }

                    return(constructedList);
                }
                else
                {
                    return(Convert.ChangeType(value, para.Type));
                }
            }
            else
            {
                if (para.IsRequired)
                {
                    throw new ParameterMissingException(para);
                }
                else
                {
                    return(null);
                }
            }
        }