2

I have a C# code and I want CIL (*.il) to contain my custom comments when I do disassembly of a source C# exe assembly.

How do I achieve it?

2
  • 1
    Just out of interest, why would you want to do that?
    – Adam
    Oct 13, 2012 at 9:47
  • This is not possible. the most practical approach is to simply ask the owner of the C# program to share the code with you. You've got a no, maybe you'll get a yes. The only way to find out is to ask. Given how easy it is to decompile an exe back to C# code, you'll get a yes more often than you think. The reason Microsoft published the source code for the .NET framework. Oct 13, 2012 at 11:11

2 Answers 2

4

Assembled IL opcodes do not contain comments, therefore you cannot disassemble and get comments out. you cannot do this, basically. Either share the IL source, or investigate whether a pdb might be possible for raw IL source.

There is no "comment" opcode.

2
  • *.il do contain them (// end of MyClass). Oct 13, 2012 at 13:12
  • 2
    @Alan yes, but an il file is not the assembled opcodes. The comments are discarded when it is assembled to opcodes, and do not exist in the assembly. Thus they cannot be disassembled back out into .il with comments. As another example, a .il file may contain whitespace - that is not preserved either Oct 13, 2012 at 13:24
1

You can write out arbitrary IL op codes using ILGenerator.Emit, but I don't think there's any support there for injecting comments.

If you just want to do this for debugging purposes (e.g. to make searching easier) you could always add an unused variable with a descriptive name. For example:

void Main()
{
    var a = new A();    
}

class A
{
    bool this_is_a_comment = false;
}

yields

IL_0001:  newobj      UserQuery+A..ctor
IL_0006:  stloc.0     

A..ctor:
IL_0000:  ldarg.0     
IL_0001:  ldc.i4.0    
IL_0002:  stfld       UserQuery+A.this_is_a_comment
IL_0007:  ldarg.0     
IL_0008:  call        System.Object..ctor
IL_000D:  nop         
IL_000E:  ret    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.