Question

I want to rename variables that defined in a method.

suppose my Method is:

public void PrintWelcome() {
    StringBuilder myMessageBuilder = new StringBuilder();
    int linesCount = 10;
    for (int i = 0; i < linesCount; i++) {
        myMessageBuilder.Append(Console.ReadLine());
        myMessageBuilder.Append(Environment.NewLine);
    }

    Console.Write(myMessageBuilder.ToString());
}

Then in another project with Mono.Cecil v 0.9.5 i open my Assembly and then i want to rename myMessageBuilder and linesCount by these codes:

foreach (MethodDefinition methodDef in typeDef.Methods) {
    if (methodDef.HasBody) {
        foreach (var variableDef in methodDef.Body.Variables) {
            variableDef.Name = GetRandomName();
        }                                    
    }

but my variables name didn't change and when i printed variableDef.Name it printed " ".

Can anybody help me for renaming my method variables?

Was it helpful?

Solution

Variables don't have names once compiled. Information like that is stored in the pdb file, not the assembly itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top