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)
|
2016-06-01 23:48:03 +03:00
|
|
|
throw new ArgumentNullException(nameof(self));
|
2016-03-22 23:02:25 +03:00
|
|
|
if (declaringTypeRef == null)
|
2016-06-01 23:48:03 +03:00
|
|
|
throw new ArgumentNullException(nameof(declaringTypeRef));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2017-02-02 12:57:50 +03:00
|
|
|
var reference = new MethodReference(self.Name, module.ImportReference(self.ReturnType))
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2017-02-02 12:57:50 +03:00
|
|
|
DeclaringType = module.ImportReference(declaringTypeRef),
|
2016-03-22 23:02:25 +03:00
|
|
|
HasThis = self.HasThis,
|
|
|
|
ExplicitThis = self.ExplicitThis,
|
|
|
|
CallingConvention = self.CallingConvention
|
|
|
|
};
|
|
|
|
|
2016-06-01 23:48:03 +03:00
|
|
|
foreach (var parameter in self.Parameters) {
|
2017-02-02 12:57:50 +03:00
|
|
|
var p = parameter.ParameterType.IsGenericParameter ? parameter.ParameterType : module.ImportReference(parameter.ParameterType);
|
2016-06-01 23:48:03 +03:00
|
|
|
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++)
|
2017-02-02 12:57:50 +03:00
|
|
|
self.Parameters[i].ParameterType = module.ImportReference(self.Parameters[i].ParameterType);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 22:39:48 +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
|
|
|
}
|
|
|
|
}
|