oleg-shilo / cs-script.npp

CS-Script (C# Intellisense) plugin for Notepad++ (x86/x64)
MIT License
246 stars 52 forks source link

Question: how does cs-script.npp avoid ILMerge use? #66

Closed martin-honnen closed 2 years ago

martin-honnen commented 2 years ago

I have a question as to how the cs-script.npp C# Notepad++ plugin works: the template for VS to build a C# plugin suggests you need to use ILMerge to merge all your referenced assemblies into one. As I am having troubling using ILMerge I am looking into how other projects work and your project seems rather complex and appears to have other dependencies, yet it doesn't seem to need to use ILMerge, instead there seems to be a subfolder in the plugins folder where you have other dlls. Can you shed some light on this?

oleg-shilo commented 2 years ago

I usually prefer a very simplistic IL merge alternative. This is how you do it.

  1. Add your dependency assemblies to the assembly resourcess.
  2. Ensure your static main does not reference any types that may trigger dependency assembly logging. Instead move the business logic in another "main" and use your real "main" as a bootstrapper with a custom assembly probing (pseudocode):

    static void Main(string[] args)
    {
      AppDomain.CurrentDomain.AssemblyResolve +=(object sender, ResolveEventArgs args)=>
      {
          if (args.Name.StartsWith("myAssemblyA,"))
              return Assembly.Load(Resources.myAssemblyA_dll));
         if (args.Name.StartsWith("myAssemblyB,"))
              return Assembly.Load(Resources.myAssemblyB_dll));
         . . .
         return null;
      };
    
      main(args);
    }
    
    static void main(string[] args)
    {
      // your app business logic
    }

That's it. If the assemblies are present then they are loaded from the file system, if they are not then from the entry assembly resources. Thusfor the distribution, you can just delete all dependency assemblies and distribute your app assembly only.

I use this technique for years and never had to resort to IL merge.

martin-honnen commented 2 years ago

Thanks a lot for your answer, that explains your approach and I hope I can try it in the future.