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:
- 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). -
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
- 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.
Leave a comment