-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicTypesSerializerResolver.cs
72 lines (61 loc) · 2.61 KB
/
BasicTypesSerializerResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
namespace CK.BinarySerialization;
/// <summary>
/// Immutable singleton that contains default serializers for well-known types.
/// <para>
/// A simple static dictionary is enough since it is only read.
/// </para>
/// </summary>
public sealed class BasicTypesSerializerResolver : ISerializerResolver
{
static readonly Dictionary<Type, ISerializationDriver> _byType;
/// <summary>
/// Gets the singleton instance.
/// </summary>
public static readonly BasicTypesSerializerResolver Instance = new BasicTypesSerializerResolver();
BasicTypesSerializerResolver() { }
static BasicTypesSerializerResolver()
{
_byType = new Dictionary<Type, ISerializationDriver>();
Register( new Serialization.DString() );
Register( new Serialization.DByteArray() );
Register( new Serialization.DBool() );
Register( new Serialization.DInt32() );
Register( new Serialization.DUInt32() );
Register( new Serialization.DInt8() );
Register( new Serialization.DUInt8() );
Register( new Serialization.DInt16() );
Register( new Serialization.DUInt16() );
Register( new Serialization.DInt64() );
Register( new Serialization.DUInt64() );
Register( new Serialization.DSingle() );
Register( new Serialization.DDouble() );
Register( new Serialization.DChar() );
Register( new Serialization.DDateTime() );
Register( new Serialization.DDateTimeOffset() );
Register( new Serialization.DTimeSpan() );
Register( new Serialization.DGuid() );
Register( new Serialization.DDecimal() );
Register( new Serialization.DSVersion() );
Register( new Serialization.DCSVersion() );
Register( new Serialization.DSVersionBound() );
Register( new Serialization.DPackageQualityVector() );
Register( new Serialization.DPackageQualityFilter() );
Register( new Serialization.DVersion() );
}
static void Register<T>( StaticValueTypeSerializer<T> driver ) where T : struct
{
_byType.Add( typeof( T ), driver );
_byType.Add( typeof( Nullable<> ).MakeGenericType( typeof( T ) ), driver.ToNullable );
}
static void Register<T>( ReferenceTypeSerializer<T> driver ) where T : class
{
_byType.Add( typeof( T ), driver.ToNullable );
}
/// <inheritdoc />
/// <remarks>
/// The <paramref name="context"/> is not used by this resolver.
/// </remarks>
public ISerializationDriver? TryFindDriver( BinarySerializerContext context, Type t ) => _byType.GetValueOrDefault( t );
}