Is it possible to internalize / IL-merge a single C # class?

I have an application with a link to a rather large library DLL (let it be called lib.dll), but it uses only one class from it (let this Helper).

lib.dllhas additional transitive links that are all copied to binmy project folder at compilation.

I would really like to avoid this overhead and find a way to "copy" or "compile" or "merge" the code that makes Helperup my main project.

Are there any possibilities for this? I would like to avoid the merger of IL lib.dllin his enthusiasm.

+4
source share
2 answers

In some situations, you can simply embed third-party DLLs as built-in resources and resolve the links yourself, as Jeffrey Richter described here .

In a nutshell, at your entry point:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
    string name = new AssemblyName(args.Name).Name;

    // Either hardcode the appropriate namespace or retrieve it at runtime in a way that makes sense for your project.
    string resourceName = string.Concat("My.Namespace.Resources.", name, ".dll");

    using(var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
        var buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);

        return Assembly.Load(buffer);
    }
};
+1
source

If you used .NET Core, a new version of .NET Linker would be an option. Otherwise, if you allow the license, and if the class you use does not have too many dependencies and the dll does not get confused, you can simply copy the code into your application by decompiling the dll using IL Spy

+1
source

Source: https://habr.com/ru/post/1684896/


All Articles