Static Linking of libraries created on C# .NET

后端 未结 5 1668
春和景丽
春和景丽 2020-12-01 02:26

I\'m using VS2008 C#.NET.

I created 3 different classes of libraries in 3 projects. I wrote an application which uses these libraries (dlls).

What is happeni

相关标签:
5条回答
  • 2020-12-01 02:37

    Stumbled across this question, and found another method of achieving these ends from Jeffrey Richter (http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx): you can embed your .dll into your executable as a resource, then overload the AssemblyResolve event to load the assembly that way. Quoting the code from the link:

    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);
       }
    };
    
    0 讨论(0)
  • 2020-12-01 02:40

    ILMerge is what you're after.

    I'm not sure I'd really call this "static linking" - it's just merging several assemblies into one. (In particular, please don't get the impression that this is building a native, unmanaged executable.) However, I think it's what you're after :)

    Update: ILMerge is now open source and is also available as a NuGet package:

    Install-Package ilmerge 
    
    0 讨论(0)
  • 2020-12-01 02:41

    You should also have a look at ILMERGE-GUI. It's a GUI wrapper for ILMerge.

    0 讨论(0)
  • 2020-12-01 02:46

    You can place all of your code into one EXE project, use a third-party linker (google .net static linker for a number of options), or use ILMerge as illustrated here.

    The third-party linkers generally also offer code obfuscation, and some can also statically link the .NET Framework.

    0 讨论(0)
  • 2020-12-01 02:53

    In regards to Jon Skeet's answer, ILRepack is also a good open-source alternative to ILMerge. You can install it from NuGet via:

    Install-Package ILRepack
    
    0 讨论(0)
提交回复
热议问题