Cleaned up TypeReaders
This commit is contained in:
33
src/Discord.Net.Commands/PrimitiveParsers.cs
Normal file
33
src/Discord.Net.Commands/PrimitiveParsers.cs
Normal 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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user