2020-05-19 03:56:25 +03:00
|
|
|
using System.Collections.Generic;
|
2016-03-22 23:02:25 +03:00
|
|
|
using Mono.Cecil;
|
2020-05-19 03:56:25 +03:00
|
|
|
using System.Maui.Xaml;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-05-19 03:56:25 +03:00
|
|
|
namespace System.Maui.Build.Tasks
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
static class MethodDefinitionExtensions
|
|
|
|
{
|
2017-04-07 00:37:59 +03:00
|
|
|
public static bool MatchXArguments(this MethodDefinition methodDef, ElementNode enode, TypeReference declaringTypeRef, ModuleDefinition module, ILContext context)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
if (!enode.Properties.ContainsKey(XmlName.xArguments))
|
2017-04-07 00:37:59 +03:00
|
|
|
return !methodDef.HasParameters;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
var arguments = new List<INode>();
|
|
|
|
var node = enode.Properties[XmlName.xArguments] as ElementNode;
|
|
|
|
if (node != null)
|
|
|
|
arguments.Add(node);
|
2017-04-07 00:37:59 +03:00
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
var list = enode.Properties[XmlName.xArguments] as ListNode;
|
|
|
|
if (list != null)
|
|
|
|
foreach (var n in list.CollectionItems)
|
|
|
|
arguments.Add(n);
|
|
|
|
|
2017-04-07 00:37:59 +03:00
|
|
|
if (methodDef.Parameters.Count != arguments.Count)
|
2016-03-22 23:02:25 +03:00
|
|
|
return false;
|
|
|
|
|
2017-04-07 00:37:59 +03:00
|
|
|
for (var i = 0; i < methodDef.Parameters.Count; i++)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2017-04-07 00:37:59 +03:00
|
|
|
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];
|
|
|
|
}
|
2016-08-15 23:06:11 +03:00
|
|
|
var argType = context.Variables [arguments [i] as IElementNode].VariableType;
|
2016-03-22 23:02:25 +03:00
|
|
|
if (!argType.InheritsFromOrImplements(paramType))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-21 11:32:58 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-12-04 21:21:36 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2017-06-21 11:32:58 +03:00
|
|
|
}
|