-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
DynamicGenericResolver.cs
377 lines (348 loc) · 16.9 KB
/
DynamicGenericResolver.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// 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.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MessagePack.Formatters;
using MessagePack.Internal;
#pragma warning disable SA1403 // File may only contain a single namespace
namespace MessagePack.Resolvers
{
public sealed class DynamicGenericResolver : IFormatterResolver
{
/// <summary>
/// The singleton instance that can be used.
/// </summary>
public static readonly DynamicGenericResolver Instance = new DynamicGenericResolver();
private DynamicGenericResolver()
{
}
public IMessagePackFormatter<T> GetFormatter<T>()
{
return FormatterCache<T>.Formatter;
}
private static class FormatterCache<T>
{
public static readonly IMessagePackFormatter<T> Formatter;
static FormatterCache()
{
Formatter = (IMessagePackFormatter<T>)DynamicGenericResolverGetFormatterHelper.GetFormatter(typeof(T));
}
}
}
}
namespace MessagePack.Internal
{
internal static class DynamicGenericResolverGetFormatterHelper
{
private static readonly Dictionary<Type, Type> FormatterMap = new Dictionary<Type, Type>()
{
{ typeof(List<>), typeof(ListFormatter<>) },
{ typeof(LinkedList<>), typeof(LinkedListFormatter<>) },
{ typeof(Queue<>), typeof(QueueFormatter<>) },
{ typeof(Stack<>), typeof(StackFormatter<>) },
{ typeof(HashSet<>), typeof(HashSetFormatter<>) },
{ typeof(ReadOnlyCollection<>), typeof(ReadOnlyCollectionFormatter<>) },
{ typeof(IList<>), typeof(InterfaceListFormatter2<>) },
{ typeof(ICollection<>), typeof(InterfaceCollectionFormatter2<>) },
{ typeof(IEnumerable<>), typeof(InterfaceEnumerableFormatter<>) },
{ typeof(Dictionary<,>), typeof(DictionaryFormatter<,>) },
{ typeof(IDictionary<,>), typeof(InterfaceDictionaryFormatter<,>) },
{ typeof(SortedDictionary<,>), typeof(SortedDictionaryFormatter<,>) },
{ typeof(SortedList<,>), typeof(SortedListFormatter<,>) },
{ typeof(ILookup<,>), typeof(InterfaceLookupFormatter<,>) },
{ typeof(IGrouping<,>), typeof(InterfaceGroupingFormatter<,>) },
{ typeof(ObservableCollection<>), typeof(ObservableCollectionFormatter<>) },
{ typeof(ReadOnlyObservableCollection<>), typeof(ReadOnlyObservableCollectionFormatter<>) },
{ typeof(IReadOnlyList<>), typeof(InterfaceReadOnlyListFormatter<>) },
{ typeof(IReadOnlyCollection<>), typeof(InterfaceReadOnlyCollectionFormatter<>) },
{ typeof(ISet<>), typeof(InterfaceSetFormatter<>) },
#if NET5_0_OR_GREATER
{ typeof(IReadOnlySet<>), typeof(InterfaceReadOnlySetFormatter<>) },
#endif
{ typeof(System.Collections.Concurrent.ConcurrentBag<>), typeof(ConcurrentBagFormatter<>) },
{ typeof(System.Collections.Concurrent.ConcurrentQueue<>), typeof(ConcurrentQueueFormatter<>) },
{ typeof(System.Collections.Concurrent.ConcurrentStack<>), typeof(ConcurrentStackFormatter<>) },
{ typeof(ReadOnlyDictionary<,>), typeof(ReadOnlyDictionaryFormatter<,>) },
{ typeof(IReadOnlyDictionary<,>), typeof(InterfaceReadOnlyDictionaryFormatter<,>) },
{ typeof(System.Collections.Concurrent.ConcurrentDictionary<,>), typeof(ConcurrentDictionaryFormatter<,>) },
{ typeof(Lazy<>), typeof(LazyFormatter<>) },
};
// Reduce IL2CPP code generate size(don't write long code in <T>)
internal static object GetFormatter(Type t)
{
TypeInfo ti = t.GetTypeInfo();
if (t.IsArray)
{
var rank = t.GetArrayRank();
if (rank == 1)
{
if (t.GetElementType() == typeof(byte))
{
// byte[] is also supported in builtin formatter.
return ByteArrayFormatter.Instance;
}
return Activator.CreateInstance(typeof(ArrayFormatter<>).MakeGenericType(t.GetElementType()));
}
else if (rank == 2)
{
return Activator.CreateInstance(typeof(TwoDimensionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
}
else if (rank == 3)
{
return Activator.CreateInstance(typeof(ThreeDimensionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
}
else if (rank == 4)
{
return Activator.CreateInstance(typeof(FourDimensionalArrayFormatter<>).MakeGenericType(t.GetElementType()));
}
else
{
return null; // not supported built-in
}
}
else if (ti.IsGenericType)
{
Type genericType = ti.GetGenericTypeDefinition();
TypeInfo genericTypeInfo = genericType.GetTypeInfo();
var isNullable = genericTypeInfo.IsNullable();
Type nullableElementType = isNullable ? ti.GenericTypeArguments[0] : null;
if (genericType == typeof(KeyValuePair<,>))
{
return CreateInstance(typeof(KeyValuePairFormatter<,>), ti.GenericTypeArguments);
}
// Tuple
else if (ti.FullName.StartsWith("System.Tuple"))
{
Type tupleFormatterType = null;
switch (ti.GenericTypeArguments.Length)
{
case 1:
tupleFormatterType = typeof(TupleFormatter<>);
break;
case 2:
tupleFormatterType = typeof(TupleFormatter<,>);
break;
case 3:
tupleFormatterType = typeof(TupleFormatter<,,>);
break;
case 4:
tupleFormatterType = typeof(TupleFormatter<,,,>);
break;
case 5:
tupleFormatterType = typeof(TupleFormatter<,,,,>);
break;
case 6:
tupleFormatterType = typeof(TupleFormatter<,,,,,>);
break;
case 7:
tupleFormatterType = typeof(TupleFormatter<,,,,,,>);
break;
case 8:
tupleFormatterType = typeof(TupleFormatter<,,,,,,,>);
break;
default:
break;
}
return CreateInstance(tupleFormatterType, ti.GenericTypeArguments);
}
// ValueTuple
else if (ti.FullName.StartsWith("System.ValueTuple"))
{
Type tupleFormatterType = null;
switch (ti.GenericTypeArguments.Length)
{
case 1:
tupleFormatterType = typeof(ValueTupleFormatter<>);
break;
case 2:
tupleFormatterType = typeof(ValueTupleFormatter<,>);
break;
case 3:
tupleFormatterType = typeof(ValueTupleFormatter<,,>);
break;
case 4:
tupleFormatterType = typeof(ValueTupleFormatter<,,,>);
break;
case 5:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,>);
break;
case 6:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,>);
break;
case 7:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,>);
break;
case 8:
tupleFormatterType = typeof(ValueTupleFormatter<,,,,,,,>);
break;
default:
break;
}
return CreateInstance(tupleFormatterType, ti.GenericTypeArguments);
}
// ArraySegment
else if (genericType == typeof(ArraySegment<>))
{
if (ti.GenericTypeArguments[0] == typeof(byte))
{
return ByteArraySegmentFormatter.Instance;
}
else
{
return CreateInstance(typeof(ArraySegmentFormatter<>), ti.GenericTypeArguments);
}
}
// Memory
else if (genericType == typeof(Memory<>))
{
if (ti.GenericTypeArguments[0] == typeof(byte))
{
return ByteMemoryFormatter.Instance;
}
else
{
return CreateInstance(typeof(MemoryFormatter<>), ti.GenericTypeArguments);
}
}
// ReadOnlyMemory
else if (genericType == typeof(ReadOnlyMemory<>))
{
if (ti.GenericTypeArguments[0] == typeof(byte))
{
return ByteReadOnlyMemoryFormatter.Instance;
}
else
{
return CreateInstance(typeof(ReadOnlyMemoryFormatter<>), ti.GenericTypeArguments);
}
}
// ReadOnlySequence
else if (genericType == typeof(ReadOnlySequence<>))
{
if (ti.GenericTypeArguments[0] == typeof(byte))
{
return ByteReadOnlySequenceFormatter.Instance;
}
else
{
return CreateInstance(typeof(ReadOnlySequenceFormatter<>), ti.GenericTypeArguments);
}
}
// Standard Nullable
else if (isNullable)
{
return CreateInstance(typeof(NullableFormatter<>), new[] { nullableElementType });
}
// Mapped formatter
else
{
Type formatterType;
if (FormatterMap.TryGetValue(genericType, out formatterType))
{
return CreateInstance(formatterType, ti.GenericTypeArguments);
}
}
}
else if (ti.IsEnum)
{
return CreateInstance(typeof(GenericEnumFormatter<>), new[] { t });
}
else
{
// NonGeneric Collection
if (t == typeof(IEnumerable))
{
return NonGenericInterfaceEnumerableFormatter.Instance;
}
else if (t == typeof(ICollection))
{
return NonGenericInterfaceCollectionFormatter.Instance;
}
else if (t == typeof(IList))
{
return NonGenericInterfaceListFormatter.Instance;
}
else if (t == typeof(IDictionary))
{
return NonGenericInterfaceDictionaryFormatter.Instance;
}
if (typeof(IList).GetTypeInfo().IsAssignableFrom(ti) && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
{
return Activator.CreateInstance(typeof(NonGenericListFormatter<>).MakeGenericType(t));
}
else if (typeof(IDictionary).GetTypeInfo().IsAssignableFrom(ti) && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
{
return Activator.CreateInstance(typeof(NonGenericDictionaryFormatter<>).MakeGenericType(t));
}
}
// check inherited types(e.g. Foo : ICollection<>, Bar<T> : ICollection<T>)
{
// generic dictionary
var dictionaryDef = ti.ImplementedInterfaces.FirstOrDefault(x => x.GetTypeInfo().IsConstructedGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>));
if (dictionaryDef != null && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
{
Type keyType = dictionaryDef.GenericTypeArguments[0];
Type valueType = dictionaryDef.GenericTypeArguments[1];
return CreateInstance(typeof(GenericDictionaryFormatter<,,>), new[] { keyType, valueType, t });
}
// generic dictionary with collection ctor
var dictionaryInterfaceDef = ti.ImplementedInterfaces.FirstOrDefault(x => x.GetTypeInfo().IsConstructedGenericType() &&
(x.GetGenericTypeDefinition() == typeof(IDictionary<,>) || x.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>)));
if (dictionaryInterfaceDef != null)
{
Type keyType = dictionaryInterfaceDef.GenericTypeArguments[0];
Type valueType = dictionaryInterfaceDef.GenericTypeArguments[1];
Type[] allowedParameterTypes = new Type[]
{
typeof(IDictionary<,>).MakeGenericType(keyType, valueType),
typeof(IReadOnlyDictionary<,>).MakeGenericType(keyType, valueType),
typeof(IEnumerable<>).MakeGenericType(typeof(KeyValuePair<,>).MakeGenericType(keyType, valueType)),
};
foreach (var constructor in ti.DeclaredConstructors)
{
ParameterInfo[] parameters = constructor.GetParameters();
if (parameters.Length == 1 &&
allowedParameterTypes.Any(allowedType => parameters[0].ParameterType.IsAssignableFrom(allowedType)))
{
return CreateInstance(typeof(GenericReadOnlyDictionaryFormatter<,,>), new[] { keyType, valueType, t });
}
}
}
// generic collection
var collectionDef = ti.ImplementedInterfaces.FirstOrDefault(x => x.GetTypeInfo().IsConstructedGenericType() && x.GetGenericTypeDefinition() == typeof(ICollection<>));
if (collectionDef != null && ti.DeclaredConstructors.Any(x => x.GetParameters().Length == 0))
{
Type elemType = collectionDef.GenericTypeArguments[0];
return CreateInstance(typeof(GenericCollectionFormatter<,>), new[] { elemType, t });
}
// generic IEnumerable collection
// looking for combination of IEnumerable<T> and constructor that takes
// enumeration of the same type
foreach (var enumerableCollectionDef in ti.ImplementedInterfaces.Where(x => x.GetTypeInfo().IsConstructedGenericType() && x.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
Type elemType = enumerableCollectionDef.GenericTypeArguments[0];
Type paramInterface = typeof(IEnumerable<>).MakeGenericType(elemType);
foreach (var constructor in ti.DeclaredConstructors)
{
var parameters = constructor.GetParameters();
if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(paramInterface))
{
return CreateInstance(typeof(GenericEnumerableFormatter<,>), new[] { elemType, t });
}
}
}
}
return null;
}
private static object CreateInstance(Type genericType, Type[] genericTypeArguments, params object[] arguments)
{
return Activator.CreateInstance(genericType.MakeGenericType(genericTypeArguments), arguments);
}
}
}