简体   繁体   中英

Inject a C# file in a DLL

I have a DLL written in C# for which I don't have the sources. I have tried different C# decompiler to modify the DLL, but they all give me errors in my attempts to recompile with the modifications, I suppose due to IL decompilation limitations. Is it possible to add a .cs file to the root of the DLL in order or inject a method to add a functionality ? PS: This is not intended to hack a software but to create a mod of a game which requires DLL modification.

Your best approach may be to just create a wrapper project around the dll to add the functionality that you want. Your code could them reference the project instead of the dll. As long as the classes aren't sealed you should be able to inherit from them.

Modifying code you don't have access to probably isn't a good idea to begin with. Especially if the dll could be updated in the future.

You can also create a new DLL with the same namespace. This might make things look as if they're in the same location, but it's not the best practice and it could be confusing since namespaces are expected to match the project/dll name.

Benjamin's solution with the wrapper seems reasonable.

The Reflexil plugin for .NET Reflector could inject a method or a class in a DLL as illustrate in a video by its creator. It prevents decompilation-compilation errors as it just injects IL code in the assembly.

To install this plugin follow these steps .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM