AliOsat Mostafavi
AliOsat Mostafavi

Reputation: 373

How to rename variables in a method by Mono.Cecil v0.9.5 in C#

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?

Upvotes: 0

Views: 1004

Answers (1)

Banksy
Banksy

Reputation: 85

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

Upvotes: 1

Related Questions