Hacking method names

| No Comments | No TrackBacks

Sometimes at rainy days of our life we can found ourself looking for a way to create something impossible, say a method containing dash in its name ;)

Well, if it seems to be impossible in one reality, try another one. It's impossible in C#, but it's possible in MSIL, so here is a hack:

  1. Disassemble your dll or executable using the MSIL Disassembler:
    ildasm.exe /out=Lib.il Lib.dll
    (Note, ildasm creates also resource file Lib.res along with Lib.il, you'll need this file afterwards).
  2. Find your method in the decompliled MSIL (Lib.il), usually it looks like
    .method public hidebysig instance string
            FunnyMethod(string s) cil managed
    
    and make its name more funny, inserting a dash (then you have to surround method's name by apostrophes to satisfy the syntax analyzer):
    .method public hidebysig instance string
            'Funny-Method'(string s) cil managed
    
  3. Now just assemble fixed MSIL file back to dll or executable using the MSIL Assembler:
    ilasm.exe Lib.il /RESOURCE=Lib.res /DLL
    

That's it, you've created Lib.dll assembly, which contains Funny-Method(string) method in your class. Of course you can't invoke this method directly, but only through reflection, but sometimes that's enough.

Oh, and last thing - it's a hack, don't use it.

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/35

Leave a comment