I have a .NET assembly which has defined a type T at compile time, and I have instantiated an object my_t as an instance of this type.
I am wondering if it is possible in .NET to use the runtime compiler services to re-compile this class, then load the new class definition into the currently executing assembly, so when I call methods off of my_t, they will use the new code.
I am not changing the signatures of any of the methods, just the method bodies.
Any .NET gurus out there know if this is possible? Thanks in advance for any help!
You can swap method implementations, but ONLY if running unsafe code is an option, and the methods you want to swap have not been NGen'd or "Zapped".
The basic procedure is pretty simple in concept: you swap the memory address of where the method's JIT'd code resides with the memory address of another method's JIT'd code (you can also force the CLR to JIT your methods in the first place, to avoid a weird prerequisite issue).
There is a great proof-of-concept project up on CodeProject.com: http://www.codeproject.com/Articles/37549/CLR-Injection-Runtime-Method-Replacer
Go check it out!
I'm not sure that is possible, but if it is, it sounds like an awfully messy solution.
Use a plugin architecture, so you can dynamically compile your generated plugin, inheriting from your plugin interface and simply load that type from your newly created assembly and inject it into your plugin container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With