If I was writing code this would be very simple. I have code that looks like:
public class SomeClass
{
public void SomeMethod(ISomeService someService)
{
}
private void AnotherMethod(ISomeService someService)
{
}
}
I can get the Method definition for both methods with the Type reference but what I'm trying to get is a call from SomeMethod to AnotherMethod added in IL so you would have the equivalent code like:
public void SomeMethod(ISomeService someService)
{
AnotherMethod(someService);
}
I'm totally lost at the moment trying to understand how to properly construct the necessary instructions.
What I have right now looks something like:
private void ProcessType(TypeDefinition typeDef)
{
var anotherMethodDef = typeDef.Methods.FirstOrDefault(x => HasMethod(x, "AnotherMethod"));
if(someMethodDef != null)
{
var someMethodDef = typeDef.Methods.First(x => HasMethod(x, "SomeMethod"));
var processor = someMethodDef.Body.GetILProcessor();
// Now I need to generate:
// AnotherMethod(someService); as a new instruction
}
}
private static bool HasMethod(MethodDefinition method, string expected) =>
method.Name == expected && method.Parameters.Count() == 1 &&
method.Parameters.First().TypeDefinition.FullName == "Contoso.ISomeService";