ASP.NET Hosting

ThinkTank - Patcher: easy assembly patching

The following picture tells most of the idea:

The idea is about a tool that would allow easily patching code (remove, add, update).

Typical scenario: you have this DLL (assembly) you'd like to modify to add some tracing code, but you don't want to (or can't) go back to the source code.

Compared to Aspect-Oriented Programming, with such a tool you can write code directly in the assembly without having to create an aspect and specify weaving parameters or creating a new project or source files. The tool displays the decompiled code, then you can update the code. The new code gets compiled and inserted in the assembly in place of the old one.

Technically, two things are needed:

  • a CLR decompiler; many IL readers exist today such as the one integrated in .NET Reflector.
  • a CLR assembly instrumentation tool such as RAIL.

I think this tool should not be very difficult to create, given that everything required is available.

The tool could be an add-in to .NET Reflector. You can get more inspiration from this presentation by Lutz Roeder.

Update: this tool could also support refactoring (eg. rename a method throughout a set of assemblies).


See other ideas in the ThinkTank

6 Comments

  • Hello,



    Here are some thoughts,



    It should be a bit more difficult if you plan to work in high level languages traduced by Reflector.



    If you work with IL :

    Should be easier to do. We can modify instructions, all we have to do is to ensure that branches are still valid. But not everyone read IL as they read C# or VB.Net.

    Then, all we have to do is to generate the modified method using Emit or Rail.



    If you plan to work in a high level language :

    You have to parse the modified code (Using CodeDom ?), compile it, then substitute the modified assembly's members in the targeted assembly. It should be done using Rail. Note that code produces by Reflector is sometimes a little tricky, I hope that CodeDom will be able to parse it.



    You just have to know that Emit is painful, and that Rail is very slow and not optimized for the moment, and it is still in early stage of development, do not expect to load assemblies like System.Web. Note that Rail acts as a decompiler too (maybe the more complete for a free, open source and available product).



    All of that would have been a lot easier if System.Reflection.* was complete.



    Please note that it will be easier too if Microsoft would release it's CCI assembly (the one used by FxCop to read assemblies).



    Evain Jb,

    AspectDNG co-developer

    Rail contributor



  • Is there a way to use Rail to write Reflector CodeModel back into an assembly file?



    Rail is very ugly compared to Reflector CodeModel but lots of cool add-ins could be build if Rail would just write Reflector CodeModel back to disk. Maybe you should implement Rail on top of those interfaces?



    I don't like CCI as much as I like the Reflector API :-(

  • Fabrice, you should update to more recent Reflector :-)

  • That's an old picture ;-)

  • Henry,



    I've not looked deeply into the new Reflector CodeModel, but it seems very clean.



    Rail first objective will now to be a little more stable I think. I do not like either its API, but for the low layer of the AOP tool I'm currently writing, it's the only one that gives me read access to things like pinvoke or customattribute data.



    And I'm only an occasional contributor, for the need of our AOP tool. I can not speack for Bruno Cabral, the leader of the project.



    I hope something will be standardized some day.

    The System.Reflection.* namespace of the 2.0 framework seems to go in this way but it is still a lot imcomplete !!!



    For the CCI I would have been happy to discuss about it with you but despite all my attempts, I've not been able to get it :c(

    I just know that one of the bad point is the use of some unmanaged code imported from fusion.dll or others.

  • A tool named Reflexil provides some of the features described here. See http://weblogs.asp.net/fmarguerie/archive/2007/09/05/Reflexil-csharp-code-injection-in-assemblies.aspx

Comments have been disabled for this content.