/// <summary> /// Registers a user-friendly type name. /// </summary> /// <typeparam name="T">Type to register the name for.</typeparam> /// <param name="value">Name to register.</param> public void RegisterUserFriendlyTypeName <T>(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentNullException(nameof(value), "Name cannot be null or empty."); } var t = typeof(T); var ti = t.GetTypeInfo(); if (!ArgumentConverters.ContainsKey(t)) { throw new InvalidOperationException("Cannot register a friendly name for a type which has no associated converter."); } UserFriendlyTypeNames[t] = value; if (!ti.IsValueType) { return; } var nullableConverterType = typeof(NullableConverter <>).MakeGenericType(t); var nullableType = typeof(Nullable <>).MakeGenericType(t); UserFriendlyTypeNames[nullableType] = value; }
/// <summary> /// Unregisters an argument converter for specified type. /// </summary> /// <typeparam name="T">Type for which to unregister the converter.</typeparam> public void UnregisterConverter <T>() { var t = typeof(T); var ti = t.GetTypeInfo(); if (ArgumentConverters.ContainsKey(t)) { ArgumentConverters.Remove(t); } if (UserFriendlyTypeNames.ContainsKey(t)) { UserFriendlyTypeNames.Remove(t); } if (!ti.IsValueType) { return; } var nullableType = typeof(Nullable <>).MakeGenericType(t); if (!ArgumentConverters.ContainsKey(nullableType)) { return; } ArgumentConverters.Remove(nullableType); UserFriendlyTypeNames.Remove(nullableType); }
/// <summary> /// Registers an argument converter for specified type. /// </summary> /// <typeparam name="T">Type for which to register the converter.</typeparam> /// <param name="converter">Converter to register.</param> public void RegisterConverter <T>(IArgumentConverter <T> converter) { if (converter == null) { throw new ArgumentNullException(nameof(converter), "Converter cannot be null."); } var t = typeof(T); var ti = t.GetTypeInfo(); ArgumentConverters[t] = converter; if (!ti.IsValueType) { return; } var nullableConverterType = typeof(NullableConverter <>).MakeGenericType(t); var nullableType = typeof(Nullable <>).MakeGenericType(t); if (ArgumentConverters.ContainsKey(nullableType)) { return; } var nullableConverter = Activator.CreateInstance(nullableConverterType) as IArgumentConverter; ArgumentConverters[nullableType] = nullableConverter; }
/// <summary> /// Unregisters an argument converter for specified type. /// </summary> /// <typeparam name="T">Type for which to unregister the converter.</typeparam> public static void UnregisterConverter <T>() { var t = typeof(T); if (ArgumentConverters.ContainsKey(t)) { ArgumentConverters.Remove(t); } }
/// <summary> /// Unregisters an argument converter for specified type. /// </summary> /// <typeparam name="T">Type for which to unregister the converter.</typeparam> public static void UnregisterConverter <T>() { var t = typeof(T); if (ArgumentConverters.ContainsKey(t)) { ArgumentConverters.Remove(t); } if (UserFriendlyTypeNames.ContainsKey(t)) { UserFriendlyTypeNames.Remove(t); } }
/// <summary> /// Registers a user-friendly type name. /// </summary> /// <typeparam name="T">Type to register the name for.</typeparam> /// <param name="value">Name to register.</param> public static void RegisterUserFriendlyTypeName <T>(string value) { if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentNullException("Name cannot be null or empty.", nameof(value)); } var t = typeof(T); if (!ArgumentConverters.ContainsKey(t)) { throw new InvalidOperationException("Cannot register a friendly name for a type which has no associated converter."); } UserFriendlyTypeNames[t] = value; }
/// <summary> /// Converts a string to specified type. /// </summary> /// <typeparam name="T">Type to convert to.</typeparam> /// <param name="value">Value to convert.</param> /// <param name="ctx">Context in which to convert to.</param> /// <returns>Converted object.</returns> public static object ConvertArgument <T>(this string value, CommandContext ctx) { var t = typeof(T); if (!ArgumentConverters.ContainsKey(t)) { throw new ArgumentException("There is no converter specified for given type.", nameof(T)); } var cv = ArgumentConverters[t] as IArgumentConverter <T>; if (cv == null) { throw new ArgumentException("Invalid converter registered for this type.", nameof(T)); } if (!cv.TryConvert(value, ctx, out var result)) { throw new ArgumentException("Could not convert specified value to given type.", nameof(value)); } return(result); }
/// <summary> /// Converts a string to specified type. /// </summary> /// <typeparam name="T">Type to convert to.</typeparam> /// <param name="value">Value to convert.</param> /// <param name="ctx">Context in which to convert to.</param> /// <returns>Converted object.</returns> public async Task <object> ConvertArgument <T>(string value, CommandContext ctx) { var t = typeof(T); if (!ArgumentConverters.ContainsKey(t)) { throw new ArgumentException("There is no converter specified for given type.", nameof(T)); } if (!(this.ArgumentConverters[t] is IArgumentConverter <T> cv)) { throw new ArgumentException("Invalid converter registered for this type.", nameof(T)); } var cvr = await cv.ConvertAsync(value, ctx).ConfigureAwait(false); if (!cvr.HasValue) { throw new ArgumentException("Could not convert specified value to given type.", nameof(value)); } return(cvr.Value); }