Repository navigation
Expand file tree
/
Copy pathGenericUtil.cs
More file actions
138 lines (129 loc) · 4.34 KB
/
Copy pathGenericUtil.cs
File metadata and controls
138 lines (129 loc) · 4.34 KB
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
using System.Linq;
using System;
using System.Collections.Generic;
using System.Resources;
namespace Python.Runtime
{
/// <summary>
/// This class is responsible for efficiently maintaining the bits
/// of information we need to support aliases with 'nice names'.
/// </summary>
internal static class GenericUtil
{
/// <summary>
/// Maps namespace -> generic base name -> list of generic type names.
/// </summary>
// Lock: nested Dict/List mutations cannot be expressed with ConcurrentDictionary alone.
private static Dictionary<string, Dictionary<string, List<string>>> mapping = new();
private static readonly object _lock = new();
public static void Reset()
{
lock (_lock)
{
mapping = new Dictionary<string, Dictionary<string, List<string>>>();
}
}
/// <summary>
/// Register a generic type that appears in a given namespace.
/// </summary>
/// <param name="t">A generic type definition (<c>t.IsGenericTypeDefinition</c> must be true)</param>
internal static void Register(Type t)
{
if (null == t.Namespace || null == t.Name)
{
return;
}
lock (_lock)
{
if (!mapping.TryGetValue(t.Namespace, out var nsmap))
{
nsmap = new Dictionary<string, List<string>>();
mapping[t.Namespace] = nsmap;
}
string basename = GetBasename(t.Name);
if (!nsmap.TryGetValue(basename, out var gnames))
{
gnames = new List<string>();
nsmap[basename] = gnames;
}
gnames.Add(t.Name);
}
}
/// <summary>
/// xxx
/// </summary>
public static List<string>? GetGenericBaseNames(string ns)
{
lock (_lock)
{
if (mapping.TryGetValue(ns, out var nsmap))
{
return nsmap.Keys.ToList();
}
return null;
}
}
/// <summary>
/// Finds a generic type with the given number of generic parameters and the same name and namespace as <paramref name="t"/>.
/// </summary>
public static Type? GenericForType(Type t, int paramCount)
{
return GenericByName(t.Namespace, t.Name, paramCount);
}
/// <summary>
/// Finds a generic type in the given namespace with the given name and number of generic parameters.
/// </summary>
public static Type? GenericByName(string ns, string basename, int paramCount)
{
// Snapshot under lock; AssemblyManager below can reenter Register.
string[]? candidates = null;
lock (_lock)
{
if (mapping.TryGetValue(ns, out var nsmap)
&& nsmap.TryGetValue(GetBasename(basename), out var names))
{
candidates = names.ToArray();
}
}
if (candidates == null) return null;
foreach (string name in candidates)
{
string qname = $"{ns}.{name}";
Type o = AssemblyManager.LookupTypes(qname).FirstOrDefault();
if (o != null && o.GetGenericArguments().Length == paramCount)
{
return o;
}
}
return null;
}
/// <summary>
/// xxx
/// </summary>
public static string? GenericNameForBaseName(string ns, string name)
{
lock (_lock)
{
if (mapping.TryGetValue(ns, out var nsmap)
&& nsmap.TryGetValue(name, out var gnames)
&& gnames.Count > 0)
{
return gnames[0];
}
return null;
}
}
private static string GetBasename(string name)
{
int tick = name.IndexOf("`");
if (tick > -1)
{
return name.Substring(0, tick);
}
else
{
return name;
}
}
}
}