maui-linux/Xamarin.Forms.Build.Tasks/MethodReferenceExtensions.cs

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

2016-03-22 23:02:25 +03:00
using System;
using Mono.Cecil;
namespace Xamarin.Forms.Build.Tasks
{
static class MethodReferenceExtensions
{
public static MethodReference ResolveGenericParameters(this MethodReference self, TypeReference declaringTypeRef,
ModuleDefinition module)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
2016-03-22 23:02:25 +03:00
if (declaringTypeRef == null)
throw new ArgumentNullException(nameof(declaringTypeRef));
2016-03-22 23:02:25 +03:00
var reference = new MethodReference(self.Name, module.ImportReference(self.ReturnType))
2016-03-22 23:02:25 +03:00
{
DeclaringType = module.ImportReference(declaringTypeRef),
2016-03-22 23:02:25 +03:00
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis,
CallingConvention = self.CallingConvention
};
foreach (var parameter in self.Parameters) {
var p = parameter.ParameterType.IsGenericParameter ? parameter.ParameterType : module.ImportReference(parameter.ParameterType);
reference.Parameters.Add(new ParameterDefinition(p));
}
2016-03-22 23:02:25 +03:00
foreach (var generic_parameter in self.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference));
return reference;
}
public static void ImportTypes(this MethodReference self, ModuleDefinition module)
{
if (self.HasParameters)
{
for (var i = 0; i < self.Parameters.Count; i++)
self.Parameters[i].ParameterType = module.ImportReference(self.Parameters[i].ParameterType);
2016-03-22 23:02:25 +03:00
}
}
public static MethodReference MakeGeneric(this MethodReference self, TypeReference declaringType, params TypeReference [] arguments)
{
var reference = new MethodReference(self.Name, self.ReturnType) {
DeclaringType = declaringType,
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis,
CallingConvention = self.CallingConvention,
};
foreach (var parameter in self.Parameters)
reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType));
foreach (var generic_parameter in self.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference));
return reference;
}
2016-03-22 23:02:25 +03:00
}
}