CodeCopier usage

May 15, 2010 at 11:32 PM

"The mutator classes have turned out to be very hard to use in practise. We are busy deprecating them"

This is the second time you mention CodeCopier exists.

How should people even know you added them?

Anyway, now that I know it exists, I'm trying to use it to complete the pseudo source generator and to merge two method bodies and I don't even understand how I should start using it.

I mean it: I don't even understand what method should I call to copy something.

I found no examples.

Not to mention the fact that when I read about "cones" I can only imagine what you are talking about but I have no precise idea.

This is the only usage I found (from it's test case): 


List<INamedTypeDefinition> ignore;
copier.AddDefinition(codeAssembly, out ignore);
codeAssembly.AllTypes = ignore;
codeAssembly = (Assembly)copier.Substitute(codeAssembly);

this piece of code is obscure at best at my eyes.

Apparently mterwoord understood how it works right away, so I must be stupid. Could you or somebody help me?


May 15, 2010 at 11:39 PM

The whole CciSharp mutating code is obsolete now, right?


May 15, 2010 at 11:52 PM
Edited May 16, 2010 at 7:31 PM

I'm copying the body of a method. I'm going to use the body content in another method. The containing type of the method will be ignore by the code I'm generating but the rest of the types in the same assembly will be referenced.


var validationBody = new Microsoft.Cci.ILToCodeModel.SourceMethodBody(validateMethod.Body, MetadataHost, null, null); // this is the original body, validateMethod is the original method

var copier = new CodeCopier(MetadataHost, null);

var ignore = new List<INamedTypeDefinition>();

// if I pass the above definition as root I get a lot of types in "ignore", I guess the copier is making fresh copies of them? yes, no? who knows?

// Anyway I should not need them because I still want to use the remaining types in the assembly. So I use the following method definition as a root. Is this correct?

copier.AddDefinition(validateMethod, out ignore);
// now ignore is empty. I assume it's a good thing.

var validationBody2 = copier.Substitute(validationBody);


Does it make sense?

Edited: it seems to work, anyway.