visual-studio 如何将程序集清单添加到 Visual Studio 2008 中的 C# .NET 类库项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3860523/
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
How to add assembly manifest to a C# .NET class library project in Visual Studio 2008?
提问by
I'm having a similar problem to what Paul had a year ago (see How to add manifest to a .NET DLL?). That is, I have a C# class library project in Visual Studio 2008, which outputs a dll. The dll references some private assemblies, so I want to add an assembly manifest to the dll that specifies those other referenced assemblies.
我遇到了与 Paul 一年前遇到的类似问题(请参阅如何将清单添加到 .NET DLL?)。也就是说,我在 Visual Studio 2008 中有一个 C# 类库项目,它输出一个 dll。dll 引用了一些私有程序集,所以我想向 dll 添加一个程序集清单,指定那些其他引用的程序集。
I know how to do this for an executable, it's just appName.exe.manifest, and when the file is included in the project, you can then just select it as the manifest in the project properties.
我知道如何为可执行文件执行此操作,它只是 appName.exe.manifest,当该文件包含在项目中时,您可以在项目属性中选择它作为清单。
According to the answer that Ruben gave Paul (in the above Stack Overflow thread), a manifest only applies to exes. However, the Microsoft documentation on manifests seems to suggest otherwise (correct me if I'm wrong), MSDN Assembly Manifests:
根据鲁本给保罗的回答(在上面的堆栈溢出线程中),清单仅适用于 exe。然而,微软关于清单的文档似乎另有建议(如果我错了,请纠正我),MSDN Assembly Manifests:
An assembly manifest is an XML file that describes a side-by-side assembly. Assembly manifests describe the names and versions of side-by-side assemblies, files, and resources of the assembly, as well as the dependence of the assembly on other side-by-side assemblies. Correct installation, activation, and execution of side-by-side assemblies requires that the assembly manifest always accompany an assembly on the system.
Because of the way side-by-side searches for private assemblies, the following naming restrictions apply when packaging a DLL as a private assembly. A recommended way of doing this is to put the assembly manifest in the DLL as a resource. In this case, the resource ID must equal 1 and the name of the private assembly may be the same as the name of the DLL. For example, if the name of the DLL is Microsoft.Windows.mysample.dll, the value of the name attribute used in the assemblyIdentity element of the manifest may also be Microsoft.Windows.mysample.
An alternate way is to put the assembly manifest in a separate file. In this case, the name of the assembly and its manifest must be different than the name of the DLL. For example, Microsoft.Windows.mysampleAsm, Microsoft.Windows.mysampleAsm.manifest, and Microsoft.Windows.Mysample.dll
程序集清单是描述并行程序集的 XML 文件。程序集清单描述并行程序集、文件和程序集资源的名称和版本,以及程序集对其他并行程序集的依赖性。并行程序集的正确安装、激活和执行要求程序集清单始终伴随系统上的程序集。
由于并排搜索私有程序集的方式,将 DLL 打包为私有程序集时适用以下命名限制。推荐的这样做的方法是将程序集清单作为资源放在 DLL 中。在这种情况下,资源 ID 必须等于 1,并且私有程序集的名称可能与 DLL 的名称相同。例如,如果 DLL 的名称是 Microsoft.Windows.mysample.dll,则清单的 assemblyIdentity 元素中使用的 name 属性的值也可能是 Microsoft.Windows.mysample。
另一种方法是将程序集清单放在一个单独的文件中。在这种情况下,程序集及其清单的名称必须与 DLL 的名称不同。例如,Microsoft.Windows.mysampleAsm、Microsoft.Windows.mysampleAsm.manifest 和 Microsoft.Windows.Mysample.dll
So I created an assembly manifest assemblyName.manifest as a separate file, and included it in the class library project. But when I go to the properties for the project, I get the same result that Paul did, the option to use your own manifest is disabled.
所以我创建了一个程序集清单 assemblyName.manifest 作为一个单独的文件,并将其包含在类库项目中。但是当我转到项目的属性时,我得到了与 Paul 相同的结果,使用您自己的清单的选项被禁用。
Am I doing something wrong? How do I add my manifest to the assembly?
难道我做错了什么?如何将清单添加到程序集?
回答by Hans Passant
What you quoted is quite inappropriate for .NET assemblies. The Windows side-by-side cache is for unmanaged DLLs, the exact equivalent in .NET is the GAC. Furthermore, the compiler already embeds references to the dependent assemblies in the assembly manifest. You can see it if you run Ildasm.exe on your assembly. Double-click the manifest, you'll see the .assembly directives listed.
您引用的内容非常不适合 .NET 程序集。Windows 并行缓存用于非托管 DLL,在 .NET 中完全等效的是 GAC。此外,编译器已经在程序集清单中嵌入了对依赖程序集的引用。如果在程序集上运行 Ildasm.exe,则可以看到它。双击清单,您将看到列出的 .assembly 指令。
Fwiw, embedding your own Windows manifest in a class library is not a problem. Just use Project + Add New Item and select the Application Manifest File template item. The auto-generated content is completely wrong for a DLL of course but it does get embedded in the DLL. You can see thatby using File + Open + File and selecting your assembly. You'll see the RT_MANIFEST with resource ID 2. Just to reiterate: don't do this for a managed DLL unless you want to enter reg-free COM directives.
Fwiw,在类库中嵌入您自己的 Windows 清单不是问题。只需使用 Project + Add New Item 并选择 Application Manifest File 模板项。当然,自动生成的内容对于 DLL 来说是完全错误的,但它确实嵌入到了 DLL 中。可以看到的是通过使用文件+打开+文件,并选择您的装配。您将看到资源 ID 为 2 的 RT_MANIFEST。重申一下:不要对托管 DLL 执行此操作,除非您想输入 reg-free COM 指令。

