Cleaned up TypeReaders

This commit is contained in:
RogueException
2016-07-26 19:01:51 -03:00
parent c8a84a05b8
commit 4a06753990
5 changed files with 100 additions and 118 deletions

View File

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