问题
When I read an assembly and rename a field that's contained inside a generic type, and then write the assembly, all references to this field inside the methods become unresolved tokens. I know that the same also happens to method references if I rename them.
// This is a generic class.
TypeDefinition type = GetSomeGenericType();
// This field is referenced from a method in this type.
type.Fields[0].Name = "_anotherName";
assemblyDefinition.Write(...);
The resulting assembly won't work. Is this the intended behavior?
I found out that if I go and manually change the instruction's operand to the FieldDefinition
instead of the FieldReference
before writing the assembly everything works. But isn't this supposed to be handled automatically by Cecil just like it handles non-generic types?
EDIT:
Turns out, when I rename the field, the reference to it from instruction operands will break as the Resolve()
method returns null then. Still on the lookout for a decent solution for this.
来源:https://stackoverflow.com/questions/27767780/mono-cecil-unresolved-tokens-when-renaming-a-generic-types-field