-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathMethodDefinitionRocks.cs
138 lines (107 loc) · 4.68 KB
/
MethodDefinitionRocks.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
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Collections.Generic;
namespace Java.Interop.Tools.Cecil {
public static class MethodDefinitionRocks
{
[Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)]
public static MethodDefinition GetBaseDefinition (this MethodDefinition method) => throw new NotSupportedException ();
public static MethodDefinition GetBaseDefinition (this MethodDefinition method, TypeDefinitionCache cache) =>
GetBaseDefinition (method, (IMetadataResolver) cache);
public static MethodDefinition GetBaseDefinition (this MethodDefinition method, IMetadataResolver resolver)
{
if (method.IsStatic || method.IsNewSlot || !method.IsVirtual)
return method;
TypeDefinition? baseType = method.DeclaringType;
while ((baseType = baseType.GetBaseType (resolver)) != null) {
foreach (var m in baseType.Methods) {
if (!m.IsConstructor &&
m.Name == method.Name &&
(m.IsVirtual || m.IsAbstract) &&
AreParametersCompatibleWith (m.Parameters, method.Parameters, resolver)) {
return m;
}
}
}
return method;
}
[Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)]
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit) => throw new NotSupportedException ();
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit, TypeDefinitionCache cache) =>
GetOverriddenMethods (method, inherit, (IMetadataResolver) cache);
public static IEnumerable<MethodDefinition> GetOverriddenMethods (MethodDefinition method, bool inherit, IMetadataResolver resolver)
{
yield return method;
if (inherit) {
MethodDefinition baseMethod = method;
while ((baseMethod = method.GetBaseDefinition (resolver)) != null && baseMethod != method) {
yield return method;
method = baseMethod;
}
}
}
[Obsolete ("Use the TypeDefinitionCache overload for better performance.", error: true)]
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b) => throw new NotSupportedException ();
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, TypeDefinitionCache cache) =>
AreParametersCompatibleWith (a, b, (IMetadataResolver) cache);
public static bool AreParametersCompatibleWith (this Collection<ParameterDefinition> a, Collection<ParameterDefinition> b, IMetadataResolver resolver)
{
if (a.Count != b.Count)
return false;
if (a.Count == 0)
return true;
for (int i = 0; i < a.Count; i++)
if (!IsParameterCompatibleWith (a [i].ParameterType, b [i].ParameterType, resolver))
return false;
return true;
}
static bool IsParameterCompatibleWith (IModifierType a, IModifierType b, IMetadataResolver cache)
{
if (!IsParameterCompatibleWith (a.ModifierType, b.ModifierType, cache))
return false;
return IsParameterCompatibleWith (a.ElementType, b.ElementType, cache);
}
static bool IsParameterCompatibleWith (TypeSpecification a, TypeSpecification b, IMetadataResolver cache)
{
if (a is GenericInstanceType)
return IsParameterCompatibleWith ((GenericInstanceType) a, (GenericInstanceType) b, cache);
if (a is IModifierType)
return IsParameterCompatibleWith ((IModifierType) a, (IModifierType) b, cache);
return IsParameterCompatibleWith (a.ElementType, b.ElementType, cache);
}
static bool IsParameterCompatibleWith (GenericInstanceType a, GenericInstanceType b, IMetadataResolver cache)
{
if (!IsParameterCompatibleWith (a.ElementType, b.ElementType, cache))
return false;
if (a.GenericArguments.Count != b.GenericArguments.Count)
return false;
if (a.GenericArguments.Count == 0)
return true;
for (int i = 0; i < a.GenericArguments.Count; i++)
if (!IsParameterCompatibleWith (a.GenericArguments [i], b.GenericArguments [i], cache))
return false;
return true;
}
static bool IsParameterCompatibleWith (TypeReference a, TypeReference b, IMetadataResolver cache)
{
if (a is TypeSpecification || b is TypeSpecification) {
if (a.GetType () != b.GetType ())
return false;
return IsParameterCompatibleWith ((TypeSpecification) a, (TypeSpecification) b, cache);
}
if (a.IsGenericParameter) {
if (b.IsGenericParameter && a.Name == b.Name)
return true;
var gpa = (GenericParameter) a;
foreach (var c in gpa.Constraints) {
if (!c.ConstraintType.IsAssignableFrom (b, cache))
return false;
}
return true;
}
return a.FullName == b.FullName;
}
}
}