using System; using System.Collections.Generic; using System.Collections.Immutable; namespace Discord.Commands { internal delegate bool TryParseDelegate(string str, out T value); internal static class PrimitiveParsers { private static readonly IReadOnlyDictionary _parsers; static PrimitiveParsers() { var parserBuilder = ImmutableDictionary.CreateBuilder(); parserBuilder[typeof(bool)] = (TryParseDelegate)bool.TryParse; parserBuilder[typeof(sbyte)] = (TryParseDelegate)sbyte.TryParse; parserBuilder[typeof(byte)] = (TryParseDelegate)byte.TryParse; parserBuilder[typeof(short)] = (TryParseDelegate)short.TryParse; parserBuilder[typeof(ushort)] = (TryParseDelegate)ushort.TryParse; parserBuilder[typeof(int)] = (TryParseDelegate)int.TryParse; parserBuilder[typeof(uint)] = (TryParseDelegate)uint.TryParse; parserBuilder[typeof(long)] = (TryParseDelegate)long.TryParse; parserBuilder[typeof(ulong)] = (TryParseDelegate)ulong.TryParse; parserBuilder[typeof(float)] = (TryParseDelegate)float.TryParse; parserBuilder[typeof(double)] = (TryParseDelegate)double.TryParse; parserBuilder[typeof(decimal)] = (TryParseDelegate)decimal.TryParse; parserBuilder[typeof(DateTime)] = (TryParseDelegate)DateTime.TryParse; parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate)DateTimeOffset.TryParse; parserBuilder[typeof(char)] = (TryParseDelegate)char.TryParse; parserBuilder[typeof(string)] = (TryParseDelegate)delegate (string str, out string value) { value = str; return true; }; _parsers = parserBuilder.ToImmutable(); } public static TryParseDelegate Get() => (TryParseDelegate)_parsers[typeof(T)]; public static Delegate Get(Type type) => _parsers[type]; } }