Is it possible to Add/Remove/Change an embedded resource in .NET DLL?
Is it 开发者_如何学Gopossible to add/remove/change an embedded resource in a .NET DLL after it has been compiled? If so, how is this done, and are there any gotchas?
Edit:
I would like to do this manually, but eventually automatically through a script in the post-build event.
Its so easy, just 3 lines of code. What you need is to reference mono.cecil.dll (google it!) and:
var targetasmdef = AssemblyFactory.GetAssembly("My.dll");
//May seach for the one you need
targetasmdef.MainModule.Resources.RemoveAt(0);
AssemblyFactory.SaveAssembly(targetasmdef, "My2.dll");
There's no way to do this in managed code. Once a resource has been embedded it becomes part of the assembly just like the compiled MSIL code is.
However, you could do this manually, like suggested by Lucero, by disassembling the DLL into a text file using ildasm, removing the resource using a text editor, and finally reassembling the DLL using ilasm.
Here's an example using a DLL with a single embedded text file:
1) Decompile the DLL into MSIL:
ildasm MyLibrary.dll /out=MyLibrary.il
2) Open the resulting MyLibrary.il
file and remove the .mresource
section:
.mresource public MyLibrary.MyResource.txt
{
// Offset: 0x00000000 Length: 0x0000000F
// WARNING: managed resource file MyLibrary.MyResource.txt created
}
3) Reassemble the DLL from the modified MyLibrary.il
file:
ilasm MyLibrary.il /dll
Yes, this is possible, by doing a roundtrip with ILDASM
and ILASM
, replacing the embedded files inbetween.
Gotchas:
- you need the strong name key file if the assembly was strong-named, or you'll not get the same assembly name in the end
- if the assembly was signed with a certificate, you need the certificate including the private key to re-sign it if needed
- obfuscated assemblies may fail to roundtrip due to name issues
精彩评论