Example #1
0
        /// <summary>
        /// Tries to retrieve the registered, stock IArgumentType implementation
        /// that describes the specified type.
        /// </summary>
        /// <param name="type">Type to look up.</param>
        /// <param name="argType">On success, receives the object that
        /// describes the specified type; receives null otherwise.</param>
        /// <returns>True on success; false otherwise.</returns>
        public static bool TryGetType(Type type, out IArgumentType argType)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // See if it's a registered, well-known type.
            if (TryGetBuiltInType(type, out argType))
            {
                return(true);
            }

            // Or possibly a type that directly implements IArgumentType itself.
            if (type.GetTypeInfo().GetInterfaces().Contains(typeof(IArgumentType)))
            {
                var constructor = type.GetTypeInfo().GetConstructor(Array.Empty <Type>());
                if (constructor == null)
                {
                    argType = null;
                    return(false);
                }

                argType = (IArgumentType)constructor.Invoke(Array.Empty <object>());
                return(true);
            }

            // Specially handle all enum types.
            if (type.GetTypeInfo().IsEnum)
            {
                argType = EnumArgumentType.Create(type);
                return(true);
            }

            // And arrays.
            if (type.IsArray)
            {
                argType = new ArrayArgumentType(type);
                return(true);
            }

            // Handle all types that implement the generic ICollection<T>
            // interface.
            if (type.GetTypeInfo().GetInterface(typeof(ICollection <>).Name) != null)
            {
                argType = new CollectionOfTArgumentType(type);
                return(true);
            }

            // Specially handle a few well-known generic types.
            if (type.GetTypeInfo().IsGenericType)
            {
                var genericTy = type.GetGenericTypeDefinition();

                if (genericTy.IsEffectivelySameAs(typeof(KeyValuePair <,>)))
                {
                    argType = new KeyValuePairArgumentType(type);
                    return(true);
                }
                else if (genericTy.IsEffectivelySameAs(typeof(CommandGroup <>)))
                {
                    argType = new CommandGroupArgumentType(type);
                    return(true);
                }

                if (type.GetTypeInfo().GetInterface("ITuple") != null)
                {
                    argType = new TupleArgumentType(type);
                    return(true);
                }
            }

            // See if it's a nullable wrapper of a type we can handle.
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                return(TryGetType(underlyingType, out argType));
            }

            argType = null;
            return(false);
        }
Example #2
0
        public void CollectionToCollection()
        {
            var listOfIntArgType = new CollectionOfTArgumentType(typeof(List<int>));

            Action invocation = () => listOfIntArgType.ToCollection(null);
            invocation.ShouldThrow<ArgumentNullException>();

            var outCollection = listOfIntArgType.ToCollection(new ArrayList(new[] { 10, -1 }));
            outCollection.Should().BeOfType<List<int>>();

            var outList = (List<int>)outCollection;
            outList.Should().HaveCount(2);
            outList.Should().ContainInOrder(10, -1);
        }
Example #3
0
        public void TryParseCollectionWithBadSyntax()
        {
            var listOfIntArgType = new CollectionOfTArgumentType(typeof(List<int>));

            object value;
            listOfIntArgType.TryParse(ArgumentParseContext.Default, "1, z", out value).Should().BeFalse();
            value.Should().BeNull();
        }
Example #4
0
        public void TryParseCollection()
        {
            var listOfIntArgType = new CollectionOfTArgumentType(typeof(List<int>));

            object value;
            listOfIntArgType.TryParse(ArgumentParseContext.Default, "1, 2", out value).Should().BeTrue();
            value.Should().BeOfType<List<int>>();

            var list = (List<int>)value;
            list.Should().HaveCount(2);
            list.Should().ContainInOrder(1, 2);
        }
Example #5
0
        public void FormatCollection()
        {
            var listOfIntArgType = new CollectionOfTArgumentType(typeof(List<int>));

            var list = new[] { 0, 10, 3, -1 }.ToList();
            listOfIntArgType.Format(list).Should().Be("0, 10, 3, -1");
        }
Example #6
0
        /// <summary>
        /// Tries to retrieve the registered, stock IArgumentType implementation
        /// that describes the specified type.
        /// </summary>
        /// <param name="type">Type to look up.</param>
        /// <param name="argType">On success, receives the object that
        /// describes the specified type; receives null otherwise.</param>
        /// <returns>True on success; false otherwise.</returns>
        public static bool TryGetType(Type type, out IArgumentType argType)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // See if it's a registered, well-known type.
            if (TryGetBuiltInType(type, out argType))
            {
                return true;
            }

            // Or possibly a type that directly implements IArgumentType itself.
            if (type.GetInterfaces().Contains(typeof(IArgumentType)))
            {
                var constructor = type.GetConstructor(new Type[] { });
                if (constructor == null)
                {
                    argType = null;
                    return false;
                }

                argType = (IArgumentType)constructor.Invoke(new object[] { });
                return true;
            }

            // Specially handle all enum types.
            if (type.IsEnum)
            {
                argType = EnumArgumentType.Create(type);
                return true;
            }

            // And arrays.
            if (type.IsArray)
            {
                argType = new ArrayArgumentType(type);
                return true;
            }

            // Handle all types that implement the generic ICollection<T>
            // interface.
            if (type.GetInterface(typeof(ICollection<>).Name) != null)
            {
                argType = new CollectionOfTArgumentType(type);
                return true;
            }

            // Specially handle KeyValuePair and Tuple types.
            if (type.IsGenericType)
            {
                var genericTy = type.GetGenericTypeDefinition();
                
                if (genericTy == typeof(KeyValuePair<,>))
                {
                    argType = new KeyValuePairArgumentType(type);
                    return true;
                }

                if (type.GetInterface("ITuple") != null)
                {
                    argType = new TupleArgumentType(type);
                    return true;
                }
            }

            // See if it's a nullable wrapper of a type we can handle.
            var underlyingType = Nullable.GetUnderlyingType(type);
            if (underlyingType != null)
            {
                return TryGetType(underlyingType, out argType);
            }

            argType = null;
            return false;
        }