I'll give more context about what I'm doing. I'm developing an AOP tool, and as part of the IL transformation, I need to extract out particular instructions from the original method into another cecil-generated method, to be passed as a callback-delegate so that the aspect can execute the original instruction during runtime. This 'transformation pattern' was "inspired" (you may say "stolen") from AspectJ, which does pretty much the same thing in principle.
All works pretty well in ordinary methods so far, except that I just realized that I've forgotten one important difference between .net dan java: .net has true generics. What it means is that I can't just copy the instruction as is to the new method (as we can do in Java), instead I need to make some extra adjustments to "repair" all references to open-generics within the copied instructions. So I wonder if there's a better way to do some analytical work on the IL instructions to find all references to open-generics, or better, if someone might have created a utility to repair generic references (Cecil.Rocks?). Thanks On Mon, May 2, 2011 at 5:25 PM, Hendry Luk <[email protected]> wrote: > Hello, > I'm using cecil to pull out several lines of code out of a method body into > a new "clone" method, and I'm encoutering a problem with updating its > generic references. E.g.: > public static void OriginalMethod<T, Y>() > { > BlahBlah1(typeof(T)); > BlahBlah2<T>.SomeField = (Y) something; > BlahBlah3<T,Y>(); > Blah4<T>.Something<Y>(); > } > > Is going to be weaved into: > > public static void OriginalMethod<T, Y>() > { > BlahBlah1(typeof(T)); > BlahBlah2<T>((Y)something); > ClonedMethod<T, Y>(); // Extracted out > Blah4<T>.Something<Y>(); > } > > public static void ClonedMethod<T, Y>() > { > BlahBlah3<T,Y>(); > } > > You see, there's one single instruction that I extract out into a > cloned-method. However, it's generic references are still pointing to the > old generic arguments (from the original method). > Is there an easy way in mono-cecil to walk through our existing > instructions for all their generic references, and re-resolve them to point > to the new generic provider (i.e. the new method)? > > PS: The shape of the instruction is quite hard to determine (e.g. I just > gave 4 possible ways of how the instruction may reference to the generics). > The only way I could think of at the moment is to handle each possible > shape of instruction operand (i.e. TypeReference, MemberReference, > GenericInstanceMethod, GenericInstanceType, etc) and try to handle every > possible way that our generic parameter might get referenced, and fix them > appropriately. But that's very error-prone. Is there any easier way to do > that? Something like walker or visitor-pattern that enables me to analyse my > instructions for any reference to the generic parameter. > > Thanks before > > > -- -- mono-cecil
