I'm trying to use Cecil to find instances of calls to a generic method using an interface for a convention test. I'm having trouble identifying the generic type from the MethodReference
.
I've set up a basic test:
private interface IAnimal
{
}
private class Duck : IAnimal
{
}
private class Farm
{
private readonly ICollection<string> _animals = new List<string>();
public void Add<T>()
{
_animals.Add(typeof(T).Name);
}
public override string ToString()
{
return string.Join(", ", _animals);
}
}
static Farm FarmFactory()
{
var farm = new Farm();
farm.Add<Duck>();
farm.Add<Duck>();
farm.Add<IAnimal>(); // whoops
farm.Add<Duck>();
return farm;
}
private static void Main(string[] args)
{
var farm = FarmFactory();
Console.WriteLine("Farm:");
Console.WriteLine(farm);
// Use Cecil to find the call to farm.Add<IAnimal>():
Console.WriteLine("Errors:");
FindErrors();
Console.Read();
}
So I want to find the call to farm.Add<IAnimal>()
, which won't give a compile time error or even a run-time error, until the farm conceivable tried to create an instance of the type through reflection. My actual use case is a convention test for a DI container.
Cecil comes in to it in the FindErrors()
method:
private static void FindErrors()
{
var methods = AssemblyDefinition.ReadAssembly(typeof (Farm).Assembly.Location)
.Modules
.SelectMany(module => module.Types)
.SelectMany(type => type.Methods)
.Where(method => method.HasBody)
.ToArray()
;
var callsToFarmDotAdd = methods
.Select(method => new
{
Name = method.Name,
MethodReferences = GetCallsToFarmDotAdd(method)
})
.Where(x => x.MethodReferences.Any())
.ToArray()
;
var testCases = callsToFarmDotAdd
.SelectMany(x => x.MethodReferences)
;
var callsInError = testCases
.Where(test => !test.GenericParameters[0].Resolve().IsClass)
;
foreach (var error in callsInError)
{
Console.WriteLine(error.FullName);
}
}
private static IEnumerable<MethodReference> GetCallsToFarmDotAdd(MethodDefinition method)
{
return method.Body.Instructions
.Where(instruction => instruction.OpCode == OpCodes.Callvirt)
.Select(instruction => (MethodReference) instruction.Operand)
.Where(methodReference => methodReference.FullName.Contains("Farm::Add"))
;
}
The callsInError
part is where I am failing to identify the generic type used when calling Farm::Add
. Specifically, the GenericParameters
property of the MethodReference
is empty, so GenericParameters[0]
gives an ArgumentOutOfRangeException
. I've explored the MethodReference
, and I'm definitely getting the calls to Farm::Add
, but I can't see anywhere that relates to the generic type being used except for the FullName
property, which isn't useful.
How can I get Cecil to identify the generic type used in the call?
If I cast the MethodReference
to a GenericInstanceMethod
the GenericArguments
parameter does what I need:
var callsInError = testCases
.Where(test => !((GenericInstanceMethod)test).GenericArguments[0].Resolve().IsClass)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With