C # 4.0 Merging .dll with .NET.

I decided to leave my other question to die, as I was thinking of a new idea using the Jeffrey Richter method written on this page to integrate the .dll library into my application. So I added my DLL file as an embedded resource, and also added it as a link. Then in Program.cs (I have no idea where the code he sent should go), I added the following:

... [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string[] args = Environment.GetCommandLineArgs(); if (args.Length > 1) _in = args[1]; SingleInstanceController controller = new SingleInstanceController(); controller.Run(args); AppDomain.CurrentDomain.AssemblyResolve += (sender, argsx) => { String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(argsx.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); } } ; 

Should I change the name of the resource to something else? Did I add it correctly (in the right place)?

Now the problem is that I still cannot find and load the assembly, and I'm not sure what I did wrong. Any help would be appreciated.

+4
source share
3 answers

Connect to the AssemblyResolve event before the AppDomain attempts to resolve the links, for example, at the entry point and on the first line.

The resource name you enter begins with the name of the application, followed by the name of the resource. ex: ConsoleApplication.Test.dll.

+1
source

Your problem is very similar to this: C #: how to embed a DLL in a resource file (there is no copy of the dll in the program directory)

Basically, the AppDomain.AssemblyResolve event handler was not called because Main could not be compiled. Even if it compiles, event handler binding should be the first thing you do basically.

My answer to the question above contains an example of working code and an explanation of the reasons why your code does not work.

+3
source

Use a debugger. Set breakpoints on the Assignment AssemblyResolve and lambda body. One step code.

Yes, it's too late. Move destination. If SingleInstanceController is in such a DLL, then the Main () method never starts. Move this code to a separate helper method and assign the [MethodImpl (MethodImplOptions.Noinlining)] attribute to it.

Distributing your program in a single file is already very well supported, it does not require any code and does not combine DLLs. Also takes care of the desktop shortcut, file associations, and .NET installation on older machines. It is called setup.exe

+2
source

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


All Articles