3

How can I get a MethodReference to a base class' method by name?

I've tried

type.BaseType.Resolve().Methods;

and if I add the dll containing the base class to the assemblyresolver it returns the methods. But if I add a call using

MSILWorker.Create(OpCodes.Call, baseMethod);

(where baseMethod was found by foreaching Methods from the resolved TypeDefinition) the resulting IL is unreadable, even Reflector freezes and quits.

Now some IL:
if calling private method on type:

 call instance void SomeNamespace.MyClass::RaisePropertyChanged(string)

if calling protected method on base type:

call instance void [OtherAssembly]BaseNamespace.BaseClass::RaisePropertyChanged(string)

So, how can I produce the latter using Mono.Cecil?

starblue
  • 55,348
  • 14
  • 97
  • 151
TDaver
  • 7,164
  • 5
  • 47
  • 94

1 Answers1

5

As you guessed, you need to get a proper MethodReference scoped for the module. So if you have:

TypeDefinition type = ...;
TypeDefintion baseType = type.BaseType.Resolve ();
MethodDefinition baseMethod = baseType.Methods.First (m => ...);

Then baseType and baseMethod are definitions from another module. You need to import a reference to the baseMethod before using it:

MethodReference baseMethodReference = type.Module.Import (baseMethod);
il.Emit (OpCodes.Call, baseMethodReference);
Jb Evain
  • 17,319
  • 2
  • 67
  • 67
  • Can you please help me with my similar problem too? http://stackoverflow.com/questions/4968755/mono-cecil-call-generic-base-class-method-from-other-assembly Thanks. – TDaver Feb 15 '11 at 08:50
  • @TDaver: you should use the mono-cecil google group instead of SO, you'd have better chances of receiving answers quickly. – Jb Evain Feb 15 '11 at 09:05