4

Well my question is almost similar to Embedding DLLs in a compiled executable but the very great answer provided here looses the compatiblity with mono runtime, though it works on windows.

So how can I use Fody (Costura) and also maintain mono compatibility. Their docs at https://github.com/Fody/Costura#contents read:

CosturaUtility is a class that gives you access to initialize the Costura system manually in your own code. This is mainly for scenarios where the module initializer doesn't work, such as libraries and Mono.

To use, call CosturaUtility.Initialize() somewhere in your code, as early as possible.

class Program {
    static Program() {
        CosturaUtility.Initialize();
    }

    static void Main(string[] args) { ... }
}

but even after initialising ConturaUtility manually it does not support mono runtime.

I dont think the error log is relevant but here it is:

Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32' or one of its dependencies.
File name: 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32' or one of its dependencies.
File name: 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32'
Rishav
  • 3,095
  • 25
  • 45

1 Answers1

0

Use this code at the start of Main() in your Program:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
   String resourceName = "AssemblyLoadingAndReflection." +
      new AssemblyName(args.Name).Name + ".dll";
   using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
      Byte[] assemblyData = new Byte[stream.Length];
      stream.Read(assemblyData, 0, assemblyData.Length);
      return Assembly.Load(assemblyData);
   }
};

Reference: https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition/

tambeen
  • 155
  • 7