예제 #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);
        }
예제 #2
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;
        }