Mono.Cecil unresolved tokens when renaming a generic type's field

跟風遠走 提交于 2019-12-12 22:08:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!