Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the whole System.Reflection
namespace in order to make existing assemblies modifiable.
System.Reflection.Emit
only allows the creation of new, dynamic assemblies. However, all the builder classes used here inherit from the basic reflection classes (e.g. TypeBuilder
inherits from System.Type
). Unfortunately, there doesn't seem to be a way to coerce an existing, dynamically loaded type into a type builder. At least, there's no official, supported way.
So what about unsupported? Does anyone know of backdoors that allow to load existing assemblies or types into such builder classes?
Mind, I am not searching for ways to modify the current assembly (this may even be an unreasonable request) but just to modify existing assemblies loaded from disc. I fear there's no such thing but I'd like to ask anyway.
In the worst case, one would have to resort to ildasm.exe
to disassemble the code and then to ilasm.exe
for reassembly but there's no toolchain (read: IL reader) contained in .NET to work with IL data (or is there?).
/EDIT:
I've got no specific use case. I'm just interested in a general-purpose solution because patching existing assemblies is a quite common task. Take obfuscators for example, or profilers, or AOP libraries (yes, the latter can be implemented differently). As I've said, it seems incredibly wasteful to be forced to rewrite large parts of the already existing infrastructure in System.Reflection
.