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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top