.net 使用程序集属性的最佳实践是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/62353/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:50:52  来源:igfitidea点击:

What are the best practices for using Assembly Attributes?

.netassembliesattributesassemblyinfo

提问by Jakub ?turc

I have a solution with multiple project. I am trying to optimize AssemblyInfo.cs files by linking one solution wide assembly info file. What are the best practices for doing this? Which attributes should be in solution wide file and which are project/assembly specific?

我有一个包含多个项目的解决方案。我试图通过链接一个解决方案范围的程序集信息文件来优化 AssemblyInfo.cs 文件。这样做的最佳做法是什么?哪些属性应该在解决方案范围的文件中,哪些是特定于项目/程序集的?



Edit: If you are interested there is a follow up question What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion?

编辑:如果您有兴趣,有一个后续问题,AssemblyVersion、AssemblyFileVersion 和 AssemblyInformationalVersion 之间有什么区别?

回答by JRoppert

We're using a global file called GlobalAssemblyInfo.cs and a local one called AssemblyInfo.cs. The global file contains the following attributes:

我们使用了一个名为 GlobalAssemblyInfo.cs 的全局文件和一个名为 AssemblyInfo.cs 的本地文件。全局文件包含以下属性:

 [assembly: AssemblyProduct("Your Product Name")]

 [assembly: AssemblyCompany("Your Company")]
 [assembly: AssemblyCopyright("Copyright ? 2008 ...")]
 [assembly: AssemblyTrademark("Your Trademark - if applicable")]

 #if DEBUG
 [assembly: AssemblyConfiguration("Debug")]
 #else
 [assembly: AssemblyConfiguration("Release")]
 #endif

 [assembly: AssemblyVersion("This is set by build process")]
 [assembly: AssemblyFileVersion("This is set by build process")]

The local AssemblyInfo.cs contains the following attributes:

本地 AssemblyInfo.cs 包含以下属性:

 [assembly: AssemblyTitle("Your assembly title")]
 [assembly: AssemblyDescription("Your assembly description")]
 [assembly: AssemblyCulture("The culture - if not neutral")]

 [assembly: ComVisible(true/false)]

 // unique id per assembly
 [assembly: Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]

You can add the GlobalAssemblyInfo.cs using the following procedure:

您可以使用以下过程添加 GlobalAssemblyInfo.cs:

  • Select Add/Existing Item...in the context menu of the project
  • Select GlobalAssemblyInfo.cs
  • Expand the Add-Button by clicking on that little down-arrow on the right hand
  • Select "Add As Link" in the buttons drop down list
  • 在项目的上下文菜单中选择Add/Existing Item...
  • 选择 GlobalAssemblyInfo.cs
  • 通过单击右侧的小向下箭头展开添加按钮
  • 在按钮下拉列表中选择“添加为链接”

回答by Krishna

In my case, we're building a product for which we have a Visual Studio solution, with various components in their own projects. The common attributes go. In the solution, there are about 35 projects, and a common assembly info (CommonAssemblyInfo.cs), which has the following attributes:

在我的例子中,我们正在构建一个产品,我们有一个 Visual Studio 解决方案,在他们自己的项目中包含各种组件。通用属性去。在解决方案中,大约有 35 个项目,以及一个公共程序集信息(CommonAssemblyInfo.cs),它具有以下属性:

[assembly: AssemblyCompany("Company")]
[assembly: AssemblyProduct("Product Name")]
[assembly: AssemblyCopyright("Copyright ? 2007 Company")]
[assembly: AssemblyTrademark("Company")]

//This shows up as Product Version in Windows Explorer
//We make this the same for all files in a particular product version. And increment it globally for all projects.
//We then use this as the Product Version in installers as well (for example built using Wix).
[assembly: AssemblyInformationalVersion("0.9.2.0")]

The other attributes such as AssemblyTitle, AssemblyVersion etc, we supply on a per-assembly basis. When building an assembly both AssemblyInfo.cs and CommonAssemblyInfo.cs are built into each assembly. This gives us the best of both worlds where you may want to have some common attributes for all projects and specific values for some others.

我们在每个程序集的基础上提供其他属性,例如 AssemblyTitle、AssemblyVersion 等。在构建程序集时,AssemblyInfo.cs 和 CommonAssemblyInfo.cs 都被构建到每个程序集中。这为我们提供了两全其美的优势,您可能希望为所有项目拥有一些共同属性,并为其他一些项目提供特定值。

Hope that helps.

希望有帮助。

回答by Scott Dorman

The solution presented by @JRoppert is almost the same as what I do. The only difference is that I put the following lines in the local AssemblyInfo.cs file as they can vary with each assembly:

@JRoppert 提出的解决方案与我所做的几乎相同。唯一的区别是我将以下几行放在本地 AssemblyInfo.cs 文件中,因为它们可能因每个程序集而异:

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
[assembly: AssemblyVersion("This is set by build process")]
[assembly: AssemblyFileVersion("This is set by build process")]
[assembly: CLSCompliant(true)]

I also (generally) use one common assembly info per solution, with the assumption that one solution is a single product line/releasable product. The common assembly info file also has:

我还(通常)为每个解决方案使用一个通用装配信息,并假设一个解决方案是单个产品线/可发布产品。通用程序集信息文件还有:

[assembly: AssemblyInformationalVersion("0.9.2.0")]

Which will set the "ProductVersion" value displayed by Windows Explorer.

这将设置 Windows 资源管理器显示的“ProductVersion”值。

回答by jlew

MSBuild Community Taskscontains a custom task called AssemblyInfo which you can use to generate your assemblyinfo.cs. It requires a little hand-editing of your csproj files to use, but is worthwhile.

MSBuild Community Tasks包含一个名为 AssemblyInfo 的自定义任务,您可以使用它来生成您的 assemblyinfo.cs。它需要对您的 csproj 文件进行一些手动编辑才能使用,但很值得。

回答by Hyman Ukleja

In my opinion using a GlobalAssemblyInfo.cs is more trouble than it's worth, because you need to modify every project file and remember to modify every new project, whereas you get an AssemblyInfo.cs by default.

在我看来,使用 GlobalAssemblyInfo.cs 比它的价值更麻烦,因为您需要修改每个项目文件并记住修改每个新项目,而默认情况下您会得到一个 AssemblyInfo.cs。

For changes to global values (i.e. Company, Product etc) the changes are usually so infrequent and simple to manage I don't think DRYshould be a consideration. Just run the following MSBuild script (dependent on the MSBuild Extension Pack) when you want to manually change the values in all projects as a one-off:

对于全局值(即公司、产品等)的更改,更改通常很少且易于管理,我认为不应考虑DRY。当您想一次性手动更改所有项目中的值时,只需运行以下 MSBuild 脚本(取决于MSBuild 扩展包):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="UpdateAssemblyInfo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <AllAssemblyInfoFiles Include="..\**\AssemblyInfo.cs" />
    </ItemGroup>

    <Import Project="MSBuild.ExtensionPack.tasks" />

  <Target Name="UpdateAssemblyInfo">
    <Message Text="%(AllAssemblyInfoFiles.FullPath)" />
    <MSBuild.ExtensionPack.Framework.AssemblyInfo 
        AssemblyInfoFiles="@(AllAssemblyInfoFiles)"
        AssemblyCompany="Company"
        AssemblyProduct="Product"
        AssemblyCopyright="Copyright"
        ... etc ...
        />
  </Target>

</Project>

回答by Cameron MacFarland

To share a file between multiple projects you can add an existing file as a link.

要在多个项目之间共享文件,您可以将现有文件添加为链接。

To do this, add an existing file, and click on "Add as Link" in the file selector.Add As Link
(source: free.fr)

为此,请添加现有文件,然后单击文件选择器中的“添加为链接”。(来源:free.fr添加为链接

As for what to put in the shared file, I would suggest putting things that would be shared across assemblies. Things like copyright, company, perhaps version.

至于在共享文件中放什么,我建议放一些可以跨程序集共享的东西。诸如版权,公司,也许是版本之类的东西。

回答by SaguiItay

Using a single AseemblyInfo.cs file for multiple projects is not recommended. The AssemblyInfo file includes information that might be relevant only for that specific assembly. The two most obvious pieces of information are the AssemblyTitleand AssemblyVersion.

不建议对多个项目使用单个 AseemblyInfo.cs 文件。AssemblyInfo 文件包含可能仅与该特定程序集相关的信息。两个最明显的信息是AssemblyTitleAssemblyVersion

A better solution might be to use targetsfile, which are handled by the MSBuild, in order to "inject" assembly attributes to more than one project.

更好的解决方案可能是使用targets由 MSBuild 处理的文件,以便将程序集属性“注入”到多个项目。

回答by John Denniston

One thing I have found useful is to generate the AssemblyVersion elements (etc) by applying token-substitution in the pre-build phase.

我发现有用的一件事是通过在预构建阶段应用令牌替换来生成 AssemblyVersion 元素(等)。

I use TortoiseSvn, and it is easy to use its SubWCRev.exeto turn a template AssemblyInfo.wcrevinto AssemblyInfo.cs. The relevant line in the template might look something like this:

我使用 TortoiseSvn,使用它很容易SubWCRev.exe将模板AssemblyInfo.wcrev转换为AssemblyInfo.cs. 模板中的相关行可能如下所示:

[assembly: AssemblyVersion("2.3.$WCREV$.$WCMODS?1:0$$WCUNVER?1:0$")]

The third element is then the revision number. I use the fourth element to check I haven't forgotten to commit any new or changed files (the fourth element is 00 if it is all OK).

第三个元素是修订号。我使用第四个元素来检查我没有忘记提交任何新的或更改的文件(如果一切正常,第四个元素是 00)。

By the way, add AssemblyInfo.wcrevto your version control and ignoreAssemblyInfo.csif you use this.

顺便说一句,添加AssemblyInfo.wcrev到您的版本控制并忽略AssemblyInfo.cs是否使用它。