Hi again My query. Is it difficult to get a MethodInfo? Mono has a MonoMethodInfo and MonoMethod class in MonoMethod.cs MonoMethod inherits from MethodInfo class. Now if you look at the Mono source in the MonoMethod.cs, they have a method called get_method_info() or something very close to it which is a native method declared in Reflection.c file.
This method actually reads a pe image just like what cecil does , extracts the method info and presents it as a MethodInfo. So don't you think it could be wonderful if we too have it in this great library Cecil :-) On Jul 2, 11:57 am, Fabian Schmied <[email protected]> wrote: > >> So I look upon you for a great solution :-) > > > I'd be sure glad to help, but your mail doesn't contain single question. > > I think the question is still how to get the MethodInfo from a > MethodDefinition in order to pass it to ilGenerator.Emit > (OpCodes.Call, ...). > > However, I don't think Cecil is the right tool for the job here. > > richa, you write: > > >> for .e.g: the user calls a Method > >> int num = Int32.Parse("245"); typed in a text editor and invokes my > >> compiler. > >> Now during the method resolution phase I use Cecil to find the method > >> with the signature and would like to generate > >> the IL instructions for this method call. > >> So somehow If I am given a MethodInfo instead of a MethodDefinition, I > >> would pass the MethodInfo to > >> System.Reflection.Emit(Opcode opcode, MethodInfo minfo); > >> and get the IL instructions generated. > > As Jb told you, there is no easy way to get a MethodInfo from a > MethodDefinition via Cecil. Cecil does not work with the CLR's > Reflection APIs; it has its own internal model, and it loads all > assemblies itself, bypassing the CLR's loading mechanism. This means, > to get a MethodInfo from a MethodDefinition, you'd have to call > Assembly.Load to load the respective assembly using Reflection, then > eg. Module.Resolve (methodDef.Token) (or something similar) to get the > MethodInfo. > > However, this is likely not the best solution because: > - you're mixing two separate libraries that aren't meant to be used > together (Cecil and Reflection/Reflection.Emit), and > - you need to load all assemblies twice, once via Cecil and once via > Reflection. > > The first point will probably cause issues when trying to go from the > MethodDefinition to the MethodInfo. For example, for resolving generic > methods, or methods of generic types, you'll need very specific > knowledge of how both Cecil and Reflection handle those cases in order > to obtain the correct MethodInfo. It just > > The second point means that your current way is just much more > inefficient than it could be. > > So, let's go back to your problem: > > >> for .e.g: the user calls a Method > >> int num = Int32.Parse("245"); typed in a text editor and invokes my > >> compiler. > >> Now during the method resolution phase I use Cecil to find the method > >> with the signature and would like to generate > >> the IL instructions for this method call. > > Since you're already dealing with Reflection.Emit, why not just use > Reflection to find the method with the signature? > > Use Type.GetMethod ("Parse", new[] { typeof (string) }), and you get > the right MethodInfo. Or use Type.DefaultBinder.SelectMethod, which > will help with overload resolution. > > This will likely give you the desired results, with less friction and > better performance. > > Regards, > Fabian -- -- mono-cecil
