Combine .dll into C # assembly?

Using # C.net 3.5

I know about ILMerge and similar methods, but I would like to use Jeffrey Richter's suggestions .

After adding this code to the constructor, there are problems with the namespace.

Jeffrey Richter Code:

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); } }; 

I installed the two DLLs as an “Embedded Resource” and added them as a link, but when I create the program, the two dlls are still “Un-embbeded”, I deleted them as a link, but then when I built the program there are errors saying : "type or namespace could not be found ... are you missing a use directive or assembly reference?" I have two namespaces added as using ... directive, then I tried to remove two links and two using directives and still errors.

here is my code:

 using System.Reflection; namespace WindowsFormsApplication2 { public partial class Ndice : Form { public Ndice() { 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); } }; InitializeComponent(); } } } 
+4
source share
3 answers

From the description in your question, some things are unclear what might be needed to answer:

  • What exactly doesn’t work? You do not execute at compile time or at runtime?
  • Can you show code that does not compile or does not work at runtime?
  • What do you mean when you say that assemblies are "not built-in"?

Basically, from what I understand, you have code that tries to use classes from two assemblies. This code is static, which means classes must be known at compile time. Or, in other words, you must have a link to these 2 assemblies in order to compile code that uses the types that are defined in them.

I don’t quite understand what went wrong when you added these assemblies as a reference. If you are concerned that you saw that they were copied to the bin / debug directory, which should not have prevented them from being built into your main assembly anyway. Thus, to check, you can try to manually remove them from bin / debug, or perhaps set them to "copy local = false".

One more thing - the error you encountered that mentions “use” and “namespaces” was not really about namespaces. This error means that the compiler did not find the required types. This was probably due to the fact that you removed the links to 2 assemblies.

+3
source

Put it in the constructor, not in InitializeComponent (). Adding the use of directives to the source file of the main class is not a problem.

+3
source
 String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll"; 

should be changed to:

 String resourceName = Application.Current.GetType().Namespace + "." + new AssemblyName(args.Name).Name + ".dll"; 
-1
source

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


All Articles