-
Notifications
You must be signed in to change notification settings - Fork 368
/
FormatterMapByType.cs
38 lines (32 loc) · 1.23 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
33
34
35
36
37
38
// 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.
using System;
using System.Collections.Concurrent;
using System.Reflection;
#nullable enable
namespace Microsoft.DotNet.Interactive.Formatting;
internal class FormatterMapByType
{
private readonly ConcurrentDictionary<(Type type, bool flag), ITypeFormatter> _formatters = new();
private readonly Type _genericDef;
private readonly string _name;
internal FormatterMapByType(Type genericDef, string name)
{
_genericDef = genericDef;
_name = name;
}
internal ITypeFormatter GetOrCreateFormatterForType(Type type, bool includeInternals)
{
return
_formatters.GetOrAdd((type, includeInternals),
tup =>
{
return
(ITypeFormatter)
_genericDef
.MakeGenericType(tup.type)
.GetMethod(_name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
.Invoke(null, new object[] { includeInternals });
});
}
}