/// <summary> /// Constructs a new object to describe the provided command group type. /// </summary> /// <param name="type">Type to describe.</param> public CommandGroupArgumentType(Type type) : base(type) { if (!type.GetTypeInfo().IsGenericType) { throw new ArgumentOutOfRangeException(nameof(type)); } var typeParams = type.GetTypeInfo().GetGenericArguments(); if (typeParams.Length != 1) { throw new ArgumentOutOfRangeException(nameof(type)); } if (!type.GetGenericTypeDefinition().IsEffectivelySameAs(typeof(CommandGroup <>)) && type.Name != "CommandGroup`1") { throw new ArgumentOutOfRangeException(nameof(type)); } _commandTypeType = typeParams[0]; if (!_commandTypeType.GetTypeInfo().IsEnum) { throw new ArgumentOutOfRangeException(nameof(type)); } _commandArgType = EnumArgumentType.Create(_commandTypeType); }
/// <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); }