I have an existing application that is closed-source and ships its own version of Mono 3.5. I want to change the behavior of the application; specifically, I want to swap out a built-in class with a new one.
Normally, one goes about this with Mono.Cecil. However, in my case the application assembly is loaded from a readonly volume which makes modifying the assembly itself very very tricky (it involves hardware hacks to produce a new volume). I can, however, get it to load an arbitrary DLL via an officially-supported mechanism, which I could in theory use to modify the assembly at runtime.
There are numerous resources for achieving runtime code injection / function hooking via the .NET Framework, but they all fail under Mono. Approaches I've seen include:
- System.Reflection.Emit.MethodRental.SwapMethodBody - not implemented in Mono
- This CodeProject sample - uses Microsoft internals
- Microsoft Fakes - more Microsoft internals
- This MSDN blog post - Even more Microsoft internals
See related questions:
- Can Mono.Cecil modify code already loaded in the AppDomain?
- Can I redirect .NET method calls to a new method at runtime?
- Dynamically replace the contents of a C# method?
I am aware that this is a pretty terrible plan. However since I don't have the sourcecode and modifying the assembly on disk is even more terrible than doing some kind of dirty runtime hack, this is the best alternative I've generated so far.