It is a simple method to combine C # programs (including multiple Dll) into one Exe,

Source: Internet
Author: User

It is a simple method to combine C # programs (including multiple Dll) into one Exe,

Some third-party DLL files are often referenced during program development, and then the compiled exe files cannot run independently from these DLL files.

However, many times we want to develop a tool that can run perfectly with only one exe. What should we do?

The following describes a simple method that can be easily implemented without writing a line of code.

Here we need a tool named Fody. Costura. Fody. Costura is a plug-in under the Fody framework and can be installed in the VS project through Nuget. After installation, You can package all the DLL (or even PDB) files that the project depends on into the EXE file.

Usage

After the build is complete, find the newly generated EXE file in the output directory of the project. You will also find that the DLL files still exist in the output directory. But don't worry. This EXE can be run independently. You can delete all these DLL files and then run the EXE.

In addition, Fody. Costura also supports some advanced features, such:

  • Temporary assembly file: automatically decompress the DLL from the EXE to the folder system before running the EXE, and then load the DLL in the conventional way.
  • Merge Unmanaged DLL: Fody. Costura can merge Unmanaged DLL, but not automatically. If your program involves an unmanaged DLL, You need to modify the Fody. Costura configuration file to explicitly tell it which Unmanaged DLL you want to merge.
  • Pre-load DLL: Fody. Costura can help you load some DLL files in advance when the program starts. You can even specify the loading sequence of these DLL files.

You need to modify the Fody. Costura configuration file to implement these advanced features. For detailed operation steps, refer to its official documentation.

Okay, the usage of Fody. Costura has been introduced. If you are curious about the implementation principle of Fody. Costura, you can continue to look at it.

 

Implementation Principles

When CLR tries to load an assembly but fails to load it, it will trigger the AppDomain. AssemblyResolve event. Our program can listen to this event and return the Assembly that the CLR is trying to load in the event's handler function, so that the program can continue to run normally.

Fody. Costura will embed all the DLL files referenced by EXE into the EXE file during project construction. When a program uses a DLL during running (at this time, the CLR cannot find the DLL file, resulting in AppDomain. assemblyResolve event is triggered) then extract the required DLL from the embedded resources of the EXE file.

The following two functions are the code for Fody. Costura to implement this logic.

1 public static void Attach () 2 {3 var currentDomain = AppDomain. currentDomain; 4 currentDomain. assemblyResolve + = (s, e) => ResolveAssembly (e. name); 5} 6 public static Assembly ResolveAssembly (string assemblyName) 7 {8 if (nullCache. containsKey (assemblyName) 9 {10 return null; 11} 12 13 var requestedAssemblyName = new AssemblyName (assemblyName); 14 15 var assembly = Common. readExistingAssembly (RequestedAssemblyName); 16 if (assembly! = Null) 17 {18 return assembly; 19} 20 21 Common. log ("Loading assembly '{0}' into the AppDomain", requestedAssemblyName); 22 23 assembly = Common. readFromEmbeddedResources (assemblyNames, symbolNames, requestedAssemblyName); 24 if (assembly = null) 25 {26 nullCache. add (assemblyName, true); 27 28 // Handles retargeted assemblies like PCL29 if (requestedAssemblyName. flags = AssemblyNameFlags. retargetable) 30 {31 assembly = Assembly. load (requestedAssemblyName); 32} 33} 34 return assembly; 35}View Code

The Attach method listens to the AppDomain. AssemblyResolve event. When the CLR fails to load an assembly, the AssemblyResolve event processing function is executed. AssemblyResolve will try to get the target Assembly from the embedded resources of the loaded Assembly through the Common. ReadFromEmbeddedResources method and return it to CLR.

You may ask when the Attach method is executed?

In fact, for C # language, CLR hides a big move-CLR can be in each module (each Assembly contains one or more modules) execute some initialization code before loading. But unfortunately, the C # language cannot control this part of the code. Fody. Costura injects the IL Code directly into the initialization function of the module inside the EXE assembly, and the IL Code actually executes the Attach method. In this way, the Attach method can be called immediately after the EXE assembly is loaded.

The above is a brief introduction to Fody. Costura's implementation principles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.