コード例 #1
0
        public ITupleValueParser GetParser(Type type, ValueParserProvider valueParsers)
        {
            var typeInfo = type.GetTypeInfo();

            if (!typeInfo.IsGenericType)
            {
                return(null);
            }
            var typeDef = typeInfo.GetGenericTypeDefinition();

            if (typeDef == typeof(Tuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool))
            {
                var innerParser = valueParsers.GetParser(typeInfo.GenericTypeArguments[1]);
                if (innerParser == null)
                {
                    return(null);
                }
                var parserType = typeof(TupleValueParser <>).MakeGenericType(typeInfo.GenericTypeArguments[1]);
                return((ITupleValueParser)Activator.CreateInstance(parserType, new object[] { innerParser }));
            }

            if (typeDef == typeof(ValueTuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool))
            {
                var innerParser = valueParsers.GetParser(typeInfo.GenericTypeArguments[1]);
                if (innerParser == null)
                {
                    return(null);
                }
                var parserType = typeof(ValueTupleValueParser <>).MakeGenericType(typeInfo.GenericTypeArguments[1]);
                return((ITupleValueParser)Activator.CreateInstance(parserType, new object[] { innerParser }));
            }

            return(null);
        }
コード例 #2
0
        public ICollectionParser?GetParser(Type type, ValueParserProvider valueParsers)
        {
            if (type.IsArray)
            {
                var elementType   = type.GetElementType();
                var elementParser = valueParsers.GetParser(elementType);

                return(new ArrayParser(elementType, elementParser, valueParsers.ParseCulture));
            }

            if (type.IsGenericType)
            {
                var typeDef       = type.GetGenericTypeDefinition();
                var elementType   = type.GetGenericArguments().First();
                var elementParser = valueParsers.GetParser(elementType);

                if (typeof(IList <>) == typeDef ||
                    typeof(IEnumerable <>) == typeDef ||
                    typeof(ICollection <>) == typeDef ||
                    typeof(IReadOnlyCollection <>) == typeDef ||
                    typeof(IReadOnlyList <>) == typeDef ||
                    typeof(List <>) == typeDef)
                {
                    return(new ListParser(elementType, elementParser, valueParsers.ParseCulture));
                }

                if (typeof(ISet <>) == typeDef ||
                    typeof(HashSet <>) == typeDef)
                {
                    return(new HashSetParser(elementType, elementParser, valueParsers.ParseCulture));
                }
            }

            return(null);
        }
コード例 #3
0
        public CommandOptionType GetOptionType(Type clrType, ValueParserProvider?valueParsers = null)
        {
            if (clrType == typeof(bool) || clrType == typeof(bool[]))
            {
                return(CommandOptionType.NoValue);
            }

            if (clrType == typeof(string))
            {
                return(CommandOptionType.SingleValue);
            }

            if (clrType.IsArray || typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(clrType))
            {
                return(CommandOptionType.MultipleValue);
            }

            var typeInfo = clrType.GetTypeInfo();

            if (typeInfo.IsEnum)
            {
                return(CommandOptionType.SingleValue);
            }

            if (typeInfo.IsGenericType)
            {
                var typeDef = typeInfo.GetGenericTypeDefinition();
                if (typeDef == typeof(Nullable <>))
                {
                    return(GetOptionType(typeInfo.GetGenericArguments().First(), valueParsers));
                }

                if (typeDef == typeof(Tuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool))
                {
                    if (GetOptionType(typeInfo.GenericTypeArguments[1], valueParsers) == CommandOptionType.SingleValue)
                    {
                        return(CommandOptionType.SingleOrNoValue);
                    }
                }

                if (typeDef == typeof(ValueTuple <,>) && typeInfo.GenericTypeArguments[0] == typeof(bool))
                {
                    if (GetOptionType(typeInfo.GenericTypeArguments[1], valueParsers) == CommandOptionType.SingleValue)
                    {
                        return(CommandOptionType.SingleOrNoValue);
                    }
                }
            }

            if (typeof(byte) == clrType ||
                typeof(short) == clrType ||
                typeof(int) == clrType ||
                typeof(long) == clrType ||
                typeof(ushort) == clrType ||
                typeof(uint) == clrType ||
                typeof(ulong) == clrType ||
                typeof(float) == clrType ||
                typeof(double) == clrType)
            {
                return(CommandOptionType.SingleValue);
            }

            if (valueParsers?.GetParser(clrType) != null)
            {
                return(CommandOptionType.SingleValue);
            }

            throw new ArgumentException("Could not determine CommandOptionType", clrType.Name);
        }
コード例 #4
0
        public CommandOptionType GetOptionType(Type clrType, ValueParserProvider?valueParsers = null)
        {
            if (clrType == typeof(bool) || clrType == typeof(bool[]))
            {
                return(CommandOptionType.NoValue);
            }

            if (clrType == typeof(string))
            {
                return(CommandOptionType.SingleValue);
            }

            if (clrType.IsArray || typeof(IEnumerable).IsAssignableFrom(clrType))
            {
                return(CommandOptionType.MultipleValue);
            }

            if (clrType.IsEnum)
            {
                return(CommandOptionType.SingleValue);
            }

            if (ReflectionHelper.IsNullableType(clrType, out var wrappedType))
            {
                return(GetOptionType(wrappedType, valueParsers));
            }

            if (ReflectionHelper.IsSpecialValueTupleType(clrType, out var wrappedType2))
            {
                if (GetOptionType(wrappedType2, valueParsers) == CommandOptionType.SingleValue)
                {
                    return(CommandOptionType.SingleOrNoValue);
                }
            }

            if (ReflectionHelper.IsSpecialTupleType(clrType, out var wrappedType3))
            {
                if (GetOptionType(wrappedType3, valueParsers) == CommandOptionType.SingleValue)
                {
                    return(CommandOptionType.SingleOrNoValue);
                }
            }

            if (typeof(byte) == clrType ||
                typeof(short) == clrType ||
                typeof(int) == clrType ||
                typeof(long) == clrType ||
                typeof(ushort) == clrType ||
                typeof(uint) == clrType ||
                typeof(ulong) == clrType ||
                typeof(float) == clrType ||
                typeof(double) == clrType)
            {
                return(CommandOptionType.SingleValue);
            }

            if (valueParsers?.GetParser(clrType) != null)
            {
                return(CommandOptionType.SingleValue);
            }

            throw new ArgumentException("Could not determine CommandOptionType", clrType.Name);
        }