Hi! There's nothing in Cecil to help with this: virtual calls are handled are runtime.
The scenario you describe is very much an ideal case where this particular virtual function call is monomorphic. It's easy to come up with a scenario where you can not know reliably which virtual function will be invoked. Thanks, Jb On Fri, Dec 18, 2015 at 2:20 AM, Pontus Frendin <[email protected]> wrote: > I'm using Cecil to inspect my program. It is working really great with one > exception. How can I use Cecil to find the implementation of an abstract > method? > > When I use the code below to inspect my program the variable > myMethodDefinition represents the abstract method AbstractBase::MyMethod() > instead of ImplementingType::MyMethod(). The later is the method that I > would like to find. Just reading the source code it is obvious that the > method CallAbstractMethod is actually calling ImplementingType::MyMethod(). > > My question: What can I do to get my Cecil code to find the implementing > method ImplementingType::MyMethod()? > > (A color coded version of the code can be found at > http://paste2.org/mILatbNn) > > internal static class Program > { > /// <summary> > /// The main method contains the Cecil code that is used to inspect > the "actual" program > /// which is implemented in the three classes ExampleProgram, > AbstractBase and ImplementingType > /// </summary> > private static void Main() > { > var assembly = > AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location); > > var exampleProgram = assembly.MainModule.Types > .Single(t => t.Name == "ExampleProgram"); > > var callingMethod = exampleProgram.Methods > .Single(m => m.Name == > "CallImplementationOfAbstractMethod"); > > var myMethodReference = > callingMethod.Body.Instructions.Where(i => i.OpCode == > OpCodes.Callvirt) > .Select(i => (MethodReference)i.Operand) > .Single(); > > var myMethodDefinition = myMethodReference.Resolve(); > } > } > > public class ExampleProgram > { > /// <summary> > /// Makes a call to the implementation of the abstract method > MyMethod > /// </summary> > public void CallImplementationOfAbstractMethod() > { > var implementingType = new ImplementingType(); > var result = implementingType.MyMethod(); > } > > abstract class AbstractBase > { > internal abstract bool MyMethod(); > } > > class ImplementingType : AbstractBase > { > internal override bool MyMethod() > { > return true; > } > } > } > > -- > -- > -- > mono-cecil > --- > You received this message because you are subscribed to the Google Groups > "mono-cecil" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. -- -- -- mono-cecil --- You received this message because you are subscribed to the Google Groups "mono-cecil" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
