If a call Instruction's Operand is a GenericInstanceMethod how do you
find the MethodDefinition of what it's calling?

I'm looking for:
System.Void CecilTest.GenClass`1::GenMethod(U)

But my call is made as:
System.Void CecilTest.GenClass`1<GenType>::GenMethod<System.Object>(!!
0)

How would I draw the dots between the two?

Regards,
Ivan



using System;
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace CecilTest
{

    class GenClass<GenType>
    {
        //public void GenMethod<U>(GenClass<U> items) where U : T
        public void GenMethod<U>(U item) {}

        public void Test()
        {
            object o = new object();
            GenMethod(o);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AssemblyDefinition asm =
AssemblyFactory.GetAssembly(Assembly.GetExecutingAssembly().Location);
            TypeDefinition td =
asm.MainModule.Types["CecilTest.GenClass`1"];

            MethodDefinition calledMethodDefinition =
td.Methods[0]; // .method public hidebysig instance void
GenMethod<U>(!!U item) cil managed

            MethodDefinition callerMethodDefinition = td.Methods[1]; //
Test Method

            foreach (Instruction instruction in
callerMethodDefinition.Body.Instructions)
                if (instruction.Operand is MethodReference)
                {
                    MethodReference calledMethodReference =
(MethodReference)instruction.Operand;
                    if (calledMethodReference.DeclaringType is
GenericInstanceType)
                        if
(((GenericInstanceType)calledMethodReference.DeclaringType).ElementType
== td)
                        {
                            MethodDefinition resolvedMethodDefinition
=
 
td.Methods.GetMethod(calledMethodReference.Name,
calledMethodReference.Parameters);

                            Console.WriteLine("Calling: " +
calledMethodDefinition);
                            Console.WriteLine("Called as: " +
calledMethodReference);
                            Console.WriteLine("Resolved: " +
resolvedMethodDefinition);
                        }
                }
        }
    }
}


--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to