This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathMethodDefinitionExtensions.cs
68 lines (60 loc) · 2.16 KB
/
MethodDefinitionExtensions.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
using System.Collections.Generic;
using Mono.Cecil;
using Xamarin.Forms.Xaml;
namespace Xamarin.Forms.Build.Tasks
{
static class MethodDefinitionExtensions
{
public static bool MatchXArguments(this MethodDefinition methodDef, ElementNode enode, TypeReference declaringTypeRef, ModuleDefinition module, ILContext context)
{
if (!enode.Properties.ContainsKey(XmlName.xArguments))
return !methodDef.HasParameters;
var arguments = new List<INode>();
var node = enode.Properties[XmlName.xArguments] as ElementNode;
if (node != null)
arguments.Add(node);
var list = enode.Properties[XmlName.xArguments] as ListNode;
if (list != null)
foreach (var n in list.CollectionItems)
arguments.Add(n);
if (methodDef.Parameters.Count != arguments.Count)
return false;
for (var i = 0; i < methodDef.Parameters.Count; i++)
{
var paramType = methodDef.Parameters[i].ParameterType;
var genParam = paramType as GenericParameter;
if (genParam != null)
{
var index = genParam.DeclaringType.GenericParameters.IndexOf(genParam);
paramType = (declaringTypeRef as GenericInstanceType).GenericArguments[index];
}
var argType = context.Variables[arguments[i] as IElementNode].VariableType;
if (!argType.InheritsFromOrImplements(paramType))
return false;
}
return true;
}
public static TypeReference ResolveGenericReturnType(this MethodDefinition self, TypeReference declaringTypeRef, ModuleDefinition module)
{
if (self == null)
throw new System.ArgumentNullException(nameof(self));
if (declaringTypeRef == null)
throw new System.ArgumentNullException(nameof(declaringTypeRef));
if (!self.ReturnType.IsGenericParameter)
return self.ReturnType;
var t = ((GenericInstanceType)declaringTypeRef).GenericArguments[((GenericParameter)self.ReturnType).Position];
return t;
}
public static bool HasCustomAttributes(this MethodDefinition self, TypeReference attribute)
{
if (!self.HasCustomAttributes)
return false;
foreach (var arg in self.CustomAttributes)
{
if (TypeRefComparer.Default.Equals(arg.AttributeType, attribute))
return true;
}
return false;
}
}
}