-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathExtensionMethodsRegistry.cs
More file actions
178 lines (149 loc) · 5.34 KB
/
Copy pathExtensionMethodsRegistry.cs
File metadata and controls
178 lines (149 loc) · 5.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Interop.BasicDescriptors;
namespace MoonSharp.Interpreter.Interop.UserDataRegistries
{
/// <summary>
/// Registry of all extension methods. Use UserData statics to access these.
/// </summary>
internal class ExtensionMethodsRegistry
{
private static object s_Lock = new object();
private static Dictionary<Type, IUserDataDescriptor> s_TypeRegistry = new Dictionary<Type, IUserDataDescriptor>();
private static MultiDictionary<string, IOverloadableMemberDescriptor> s_Registry = new MultiDictionary<string, IOverloadableMemberDescriptor>();
private static MultiDictionary<string, UnresolvedGenericMethod> s_UnresolvedGenericsRegistry = new MultiDictionary<string, UnresolvedGenericMethod>();
private static int s_ExtensionMethodChangeVersion = 0;
private class UnresolvedGenericMethod
{
public readonly MethodInfo Method;
public readonly InteropAccessMode AccessMode;
public readonly HashSet<Type> AlreadyAddedTypes = new HashSet<Type>();
public UnresolvedGenericMethod(MethodInfo mi, InteropAccessMode mode)
{
AccessMode = mode;
Method = mi;
}
}
/// <summary>
/// Registers an extension Type (that is a type containing extension methods)
/// </summary>
/// <param name="type">The type.</param>
/// <param name="mode">The InteropAccessMode.</param>
public static void RegisterExtensionType(Type type, InteropAccessMode mode = InteropAccessMode.Default)
{
lock (s_Lock)
{
bool changesDone = false;
foreach (MethodInfo mi in type.GetMethods().Where(_mi => _mi.IsStatic))
{
if (mi.GetCustomAttributes(typeof(ExtensionAttribute), false).Length == 0)
continue;
if (mi.ContainsGenericParameters)
{
s_UnresolvedGenericsRegistry.Add(mi.Name, new UnresolvedGenericMethod(mi, mode));
changesDone = true;
continue;
}
if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
continue;
var desc = new MethodMemberDescriptor(mi, mode);
s_Registry.Add(mi.Name, desc);
changesDone = true;
}
if (changesDone)
++s_ExtensionMethodChangeVersion;
}
}
/// <summary>
/// Gets all the extension methods which can match a given name
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public static IEnumerable<IOverloadableMemberDescriptor> GetExtensionMethodsByName(string name)
{
lock (s_Lock)
return new List<IOverloadableMemberDescriptor>(s_Registry.Find(name));
}
/// <summary>
/// Gets a number which gets incremented everytime the extension methods registry changes.
/// Use this to invalidate caches based on extension methods
/// </summary>
/// <returns></returns>
public static int GetExtensionMethodsChangeVersion()
{
return s_ExtensionMethodChangeVersion;
}
/// <summary>
/// Gets all the extension methods which can match a given name and extending a given Type
/// </summary>
/// <param name="name">The name.</param>
/// <param name="extendedType">The extended type.</param>
/// <returns></returns>
public static List<IOverloadableMemberDescriptor> GetExtensionMethodsByNameAndType(string name, Type extendedType)
{
List<UnresolvedGenericMethod> unresolvedGenerics = null;
lock (s_Lock)
{
unresolvedGenerics = s_UnresolvedGenericsRegistry.Find(name).ToList();
}
foreach (UnresolvedGenericMethod ugm in unresolvedGenerics)
{
ParameterInfo[] args = ugm.Method.GetParameters();
if (args.Length == 0) continue;
Type extensionType = args[0].ParameterType;
Type genericType = GetGenericMatch(extensionType, extendedType);
if (ugm.AlreadyAddedTypes.Add(genericType))
{
if (genericType != null)
{
MethodInfo mi = InstantiateMethodInfo(ugm.Method, extensionType, genericType, extendedType);
if (mi != null)
{
if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
continue;
var desc = new MethodMemberDescriptor(mi, ugm.AccessMode);
s_Registry.Add(ugm.Method.Name, desc);
++s_ExtensionMethodChangeVersion;
}
}
}
}
return s_Registry.Find(name)
.Where(d => d.ExtensionMethodType != null && d.ExtensionMethodType.IsAssignableFrom(extendedType))
.ToList();
}
private static MethodInfo InstantiateMethodInfo(MethodInfo mi, Type extensionType, Type genericType, Type extendedType)
{
Type[] defs = mi.GetGenericArguments();
Type[] tdefs = genericType.GetGenericArguments();
if (tdefs.Length == defs.Length)
{
return mi.MakeGenericMethod(tdefs);
}
return null;
}
private static Type GetGenericMatch(Type extensionType, Type extendedType)
{
if (!extensionType.IsGenericParameter)
{
extensionType = extensionType.GetGenericTypeDefinition();
foreach (Type t in extendedType.GetAllImplementedTypes())
{
if (t.IsGenericType && t.GetGenericTypeDefinition() == extensionType)
{
return t;
}
}
}
/*if (extendedType.IsGenericParameter)
{
return extendedType;
}*/
return null;
}
}
}