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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using MessagePack.Formatters;
using MessagePack.Internal;
namespace MessagePack
{
/// <summary>
/// Allows querying for a formatter for serializing or deserializing a particular <see cref="Type" />.
/// </summary>
public interface IFormatterResolver
{
/// <summary>
/// Gets an <see cref="IMessagePackFormatter{T}"/> instance that can serialize or deserialize some type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of value to be serialized or deserialized.</typeparam>
/// <returns>A formatter, if this resolver supplies one for type <typeparamref name="T"/>; otherwise <c>null</c>.</returns>
IMessagePackFormatter<T> GetFormatter<T>();
}
public static class FormatterResolverExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IMessagePackFormatter<T> GetFormatterWithVerify<T>(this IFormatterResolver resolver)
{
if (resolver is null)
{
throw new ArgumentNullException(nameof(resolver));
}
IMessagePackFormatter<T> formatter;
try
{
formatter = resolver.GetFormatter<T>();
}
catch (TypeInitializationException ex)
{
// The fact that we're using static constructors to initialize this is an internal detail.
// Rethrow the inner exception if there is one.
// Do it carefully so as to not stomp on the original callstack.
Throw(ex);
return default; // not reachable
}
if (formatter == null)
{
Throw(typeof(T), resolver);
}
return formatter;
}
private static void Throw(TypeInitializationException ex)
{
ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
}
private static void Throw(Type t, IFormatterResolver resolver)
{
throw new FormatterNotRegisteredException(t.FullName + " is not registered in resolver: " + resolver.GetType());
}
private static readonly ThreadsafeTypeKeyHashTable<Func<IFormatterResolver, IMessagePackFormatter>> FormatterGetters =
new ThreadsafeTypeKeyHashTable<Func<IFormatterResolver, IMessagePackFormatter>>();
private static readonly MethodInfo GetFormatterRuntimeMethod = typeof(IFormatterResolver).GetRuntimeMethod(nameof(IFormatterResolver.GetFormatter), Type.EmptyTypes);
public static object GetFormatterDynamic(this IFormatterResolver resolver, Type type)
{
if (resolver is null)
{
throw new ArgumentNullException(nameof(resolver));
}
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
if (!FormatterGetters.TryGetValue(type, out var formatterGetter))
{
var genericMethod = GetFormatterRuntimeMethod.MakeGenericMethod(type);
var inputResolver = Expression.Parameter(typeof(IFormatterResolver), "inputResolver");
formatterGetter = Expression.Lambda<Func<IFormatterResolver, IMessagePackFormatter>>(
Expression.Call(inputResolver, genericMethod), inputResolver).Compile();
FormatterGetters.TryAdd(type, formatterGetter);
}
return formatterGetter(resolver);
}
internal static object GetFormatterDynamicWithVerify(this IFormatterResolver resolver, Type type)
{
var result = GetFormatterDynamic(resolver, type);
if (result == null)
{
Throw(type, resolver);
}
return result;
}
}
public class FormatterNotRegisteredException : MessagePackSerializationException
{
public FormatterNotRegisteredException(string message)
: base(message)
{
}
}
}
|