Mutator, MethodDefinition and method body

Topics: Metadata Model, Source Model
May 4, 2010 at 3:16 PM

I'm using a code mutator to implement my "pseudo-source" idea that I described in another discussion.

I mutate all nodes that have locations to add the respective location in the file generate by the C# source emitter.

When I have to mutate method definition, I call GetMutableCopy(methodDefinition);. It will return a new MethodDefinition which is initialized using the Copy() method. Copy(), among the other things, will set the Body property to the same value of the original method.

Now, method bodies have a reference to the defining method so the new MethodDefinition's Body property will contain a reference to a method body that has a reference to another method definition.

I don't see a place where this is fixed and in fact even if I add a location to the MethodDefinition, when the Body.GenerateIL() is called no location is available in the referenced defining method.


May 4, 2010 at 5:15 PM
Edited May 5, 2010 at 1:05 AM

How can MethodDefinition.Copy() (but I guess this is true for other Copy() methods) put the object in something that is not a corrupt state?


May 5, 2010 at 7:26 AM

The mutator visitors are tricky to use because the object model is in an inconsistent state while it is being mutated, which creates numerous pitfalls.

As a result, we now encourage people to instead use the MetadataCopier (or CodeCopier if youre using the CodeModel) class to make a mutable copy of the model you're working on. Then use the MutatingVisitor (or CodeMutatingVisitor) class to mutate the copy "in place".