|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +namespace Microsoft.AspNetCore.Components.Endpoints.Binding; |
| 5 | + |
| 6 | +internal static class WellKnownConverters |
| 7 | +{ |
| 8 | + public static readonly IReadOnlyDictionary<Type, FormDataConverter> Converters; |
| 9 | + |
| 10 | + static WellKnownConverters() |
| 11 | + { |
| 12 | + var converters = new Dictionary<Type, FormDataConverter> |
| 13 | + { |
| 14 | + // For the most common types, we avoid going through the factories and just |
| 15 | + // create the converters directly. This is a performance optimization. |
| 16 | + { typeof(string), new ParsableConverter<string>() }, |
| 17 | + { typeof(char), new ParsableConverter<char>() }, |
| 18 | + { typeof(bool), new ParsableConverter<bool>() }, |
| 19 | + { typeof(byte), new ParsableConverter<byte>() }, |
| 20 | + { typeof(sbyte), new ParsableConverter<sbyte>() }, |
| 21 | + { typeof(ushort), new ParsableConverter<ushort>() }, |
| 22 | + { typeof(uint), new ParsableConverter<uint>() }, |
| 23 | + { typeof(ulong), new ParsableConverter<ulong>() }, |
| 24 | + { typeof(Int128), new ParsableConverter<Int128>() }, |
| 25 | + { typeof(short), new ParsableConverter<short>() }, |
| 26 | + { typeof(int), new ParsableConverter<int>() }, |
| 27 | + { typeof(long), new ParsableConverter<long>() }, |
| 28 | + { typeof(UInt128), new ParsableConverter<UInt128>() }, |
| 29 | + { typeof(Half), new ParsableConverter<Half>() }, |
| 30 | + { typeof(float), new ParsableConverter<float>() }, |
| 31 | + { typeof(double), new ParsableConverter<double>() }, |
| 32 | + { typeof(decimal), new ParsableConverter<decimal>() }, |
| 33 | + { typeof(DateOnly), new ParsableConverter<DateOnly>() }, |
| 34 | + { typeof(DateTime), new ParsableConverter<DateTime>() }, |
| 35 | + { typeof(DateTimeOffset), new ParsableConverter<DateTimeOffset>() }, |
| 36 | + { typeof(TimeSpan), new ParsableConverter<TimeSpan>() }, |
| 37 | + { typeof(TimeOnly), new ParsableConverter<TimeOnly>() }, |
| 38 | + { typeof(Guid), new ParsableConverter<Guid>() } |
| 39 | + }; |
| 40 | + |
| 41 | + converters.Add(typeof(char?), new NullableConverter<char>((FormDataConverter<char>)converters[typeof(char)])); |
| 42 | + converters.Add(typeof(bool?), new NullableConverter<bool>((FormDataConverter<bool>)converters[typeof(bool)])); |
| 43 | + converters.Add(typeof(byte?), new NullableConverter<byte>((FormDataConverter<byte>)converters[typeof(byte)])); |
| 44 | + converters.Add(typeof(sbyte?), new NullableConverter<sbyte>((FormDataConverter<sbyte>)converters[typeof(sbyte)])); |
| 45 | + converters.Add(typeof(ushort?), new NullableConverter<ushort>((FormDataConverter<ushort>)converters[typeof(ushort)])); |
| 46 | + converters.Add(typeof(uint?), new NullableConverter<uint>((FormDataConverter<uint>)converters[typeof(uint)])); |
| 47 | + converters.Add(typeof(ulong?), new NullableConverter<ulong>((FormDataConverter<ulong>)converters[typeof(ulong)])); |
| 48 | + converters.Add(typeof(Int128?), new NullableConverter<Int128>((FormDataConverter<Int128>)converters[typeof(Int128)])); |
| 49 | + converters.Add(typeof(short?), new NullableConverter<short>((FormDataConverter<short>)converters[typeof(short)])); |
| 50 | + converters.Add(typeof(int?), new NullableConverter<int>((FormDataConverter<int>)converters[typeof(int)])); |
| 51 | + converters.Add(typeof(long?), new NullableConverter<long>((FormDataConverter<long>)converters[typeof(long)])); |
| 52 | + converters.Add(typeof(UInt128?), new NullableConverter<UInt128>((FormDataConverter<UInt128>)converters[typeof(UInt128)])); |
| 53 | + converters.Add(typeof(Half?), new NullableConverter<Half>((FormDataConverter<Half>)converters[typeof(Half)])); |
| 54 | + converters.Add(typeof(float?), new NullableConverter<float>((FormDataConverter<float>)converters[typeof(float)])); |
| 55 | + converters.Add(typeof(double?), new NullableConverter<double>((FormDataConverter<double>)converters[typeof(double)])); |
| 56 | + converters.Add(typeof(decimal?), new NullableConverter<decimal>((FormDataConverter<decimal>)converters[typeof(decimal)])); |
| 57 | + converters.Add(typeof(DateOnly?), new NullableConverter<DateOnly>((FormDataConverter<DateOnly>)converters[typeof(DateOnly)])); |
| 58 | + converters.Add(typeof(DateTime?), new NullableConverter<DateTime>((FormDataConverter<DateTime>)converters[typeof(DateTime)])); |
| 59 | + converters.Add(typeof(DateTimeOffset?), new NullableConverter<DateTimeOffset>((FormDataConverter<DateTimeOffset>)converters[typeof(DateTimeOffset)])); |
| 60 | + converters.Add(typeof(TimeSpan?), new NullableConverter<TimeSpan>((FormDataConverter<TimeSpan>)converters[typeof(TimeSpan)])); |
| 61 | + converters.Add(typeof(TimeOnly?), new NullableConverter<TimeOnly>((FormDataConverter<TimeOnly>)converters[typeof(TimeOnly)])); |
| 62 | + converters.Add(typeof(Guid?), new NullableConverter<Guid>((FormDataConverter<Guid>)converters[typeof(Guid)])); |
| 63 | + |
| 64 | + Converters = converters; |
| 65 | + } |
| 66 | +} |
0 commit comments