在 C# .NET 上创建的库的静态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1868449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static Linking of libraries created on C# .NET
提问by claws
I'm using VS2008 C#.NET.
我正在使用 VS2008 C#.NET。
I created 3 different classes of libraries in 3 projects. I wrote an application which uses these libraries (dlls).
我在 3 个项目中创建了 3 个不同类别的库。我编写了一个使用这些库(dll)的应用程序。
What is happening is each project is compiling into a class library. So, I've 3 dlls and 1 exe.
发生的事情是每个项目都编译成一个类库。所以,我有 3 个 dll 和 1 个 exe。
Instead I want to have these in two ways:
相反,我想以两种方式拥有这些:
- Only class library assembly (dll) which contains 3 of them and 1 exe.
- just one EXE (everything inside it) :: static linking.
- 只有包含其中 3 个和 1 个 exe 的类库程序集(dll)。
- 只有一个 EXE(里面的所有东西):: 静态链接。
How could I do that? I cannot find any options for static linking in VS2008 also please mention commandline options too.
我怎么能那样做?我在 VS2008 中找不到任何静态链接选项,也请提及命令行选项。
采纳答案by Jon Skeet
ILMergeis what you're after.
ILMerge正是您所追求的。
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 sourceand is also available as a NuGet package:
更新:ILMerge 现在是开源的,也可以作为NuGet 包使用:
Install-Package ilmerge
回答by Eric J.
You can place all of your code into one EXE project, use a third-party linker (google .net static linkerfor a number of options), or use ILMerge as illustrated here.
您可以将所有代码放入一个 EXE 项目中,使用第三方链接器(google .net 静态链接器提供多种选项),或使用 ILMerge,如下所示。
The third-party linkers generally also offer code obfuscation, and some can also statically link the .NET Framework.
第三方链接器通常也提供代码混淆,有些还可以静态链接 .NET Framework。
回答by Paul Hutchinson
You should also have a look at ILMERGE-GUI. It's a GUI wrapper for ILMerge.
您还应该看看ILMERGE-GUI。它是 ILMerge 的 GUI 包装器。
回答by Aidan Cully
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:
偶然发现了这个问题,并从 Jeffrey Richter ( http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr -via-c-third-edition.aspx):您可以将 .dll 作为资源嵌入到可执行文件中,然后重载AssemblyResolve
事件以这种方式加载程序集。引用链接中的代码:
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);
}
};
回答by James Ko
In regards to Jon Skeet'sanswer, ILRepack is also a good open-sourcealternative to ILMerge. You can install it from NuGet via:
关于Jon Skeet 的回答,ILRepack 也是ILMerge 的一个很好的开源替代品。您可以通过以下方式从 NuGet 安装它:
Install-Package ILRepack