You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrimitiveParsers.cs 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. namespace Discord.Commands
  5. {
  6. internal delegate bool TryParseDelegate<T>(string str, out T value);
  7. internal static class PrimitiveParsers
  8. {
  9. private static readonly Lazy<IReadOnlyDictionary<Type, Delegate>> _parsers = new Lazy<IReadOnlyDictionary<Type, Delegate>>(CreateParsers);
  10. public static IEnumerable<Type> SupportedTypes = _parsers.Value.Keys;
  11. static IReadOnlyDictionary<Type, Delegate> CreateParsers()
  12. {
  13. var parserBuilder = ImmutableDictionary.CreateBuilder<Type, Delegate>();
  14. parserBuilder[typeof(bool)] = (TryParseDelegate<bool>)bool.TryParse;
  15. parserBuilder[typeof(sbyte)] = (TryParseDelegate<sbyte>)sbyte.TryParse;
  16. parserBuilder[typeof(byte)] = (TryParseDelegate<byte>)byte.TryParse;
  17. parserBuilder[typeof(short)] = (TryParseDelegate<short>)short.TryParse;
  18. parserBuilder[typeof(ushort)] = (TryParseDelegate<ushort>)ushort.TryParse;
  19. parserBuilder[typeof(int)] = (TryParseDelegate<int>)int.TryParse;
  20. parserBuilder[typeof(uint)] = (TryParseDelegate<uint>)uint.TryParse;
  21. parserBuilder[typeof(long)] = (TryParseDelegate<long>)long.TryParse;
  22. parserBuilder[typeof(ulong)] = (TryParseDelegate<ulong>)ulong.TryParse;
  23. parserBuilder[typeof(float)] = (TryParseDelegate<float>)float.TryParse;
  24. parserBuilder[typeof(double)] = (TryParseDelegate<double>)double.TryParse;
  25. parserBuilder[typeof(decimal)] = (TryParseDelegate<decimal>)decimal.TryParse;
  26. parserBuilder[typeof(DateTime)] = (TryParseDelegate<DateTime>)DateTime.TryParse;
  27. parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate<DateTimeOffset>)DateTimeOffset.TryParse;
  28. //parserBuilder[typeof(TimeSpan)] = (TryParseDelegate<TimeSpan>)TimeSpan.TryParse;
  29. parserBuilder[typeof(char)] = (TryParseDelegate<char>)char.TryParse;
  30. return parserBuilder.ToImmutable();
  31. }
  32. public static TryParseDelegate<T> Get<T>() => (TryParseDelegate<T>)_parsers.Value[typeof(T)];
  33. public static Delegate Get(Type type) => _parsers.Value[type];
  34. }
  35. }