-
Notifications
You must be signed in to change notification settings - Fork 368
/
FormatterMapByType.cs
32 lines (26 loc) · 1.12 KB
/
FormatterMapByType.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Reflection;
namespace Microsoft.DotNet.Interactive.Formatting;
internal class FormatterMapByType
{
private readonly ConcurrentDictionary<Type, ITypeFormatter> _formatters = new();
private readonly Type _genericDef;
private readonly string _createFormatterMethodName;
internal FormatterMapByType(Type genericDef, string createFormatterMethodName)
{
_genericDef = genericDef;
_createFormatterMethodName = createFormatterMethodName;
}
internal ITypeFormatter GetOrCreateFormatterForType(Type type) =>
_formatters.GetOrAdd(
type,
t => (ITypeFormatter)
_genericDef
.MakeGenericType(t)
.GetMethod(_createFormatterMethodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
.Invoke(null, Array.Empty<object>()));
}