maui-linux/System.Maui.Build.Tasks/MethodDefinitionExtensions.cs

67 строки
2.1 KiB
C#
Исходник Обычный вид История

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
{
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))
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);
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);
if (methodDef.Parameters.Count != arguments.Count)
2016-03-22 23:02:25 +03:00
return false;
for (var i = 0; i < methodDef.Parameters.Count; i++)
2016-03-22 23:02:25 +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];
}
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;
}
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;
}
2016-03-22 23:02:25 +03:00
}
}