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