简体   繁体   English

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

[英]What are the best practices for using Assembly Attributes?

I have a solution with multiple project.我有一个包含多个项目的解决方案。 I am trying to optimize AssemblyInfo.cs files by linking one solution wide assembly info file.我正在尝试通过链接一个解决方案范围的程序集信息文件来优化 AssemblyInfo.cs 文件。 What are the best practices for doing this?这样做的最佳做法是什么? Which attributes should be in solution wide file and which are project/assembly specific?哪些属性应该在解决方案范围的文件中,哪些是项目/程序集特定的?


Edit: If you are interested there is a follow up question What are differences between AssemblyVersion, AssemblyFileVersion and AssemblyInformationalVersion?编辑:如果您有兴趣,有一个后续问题AssemblyVersion、AssemblyFileVersion 和 AssemblyInformationalVersion 之间有什么区别?

We're using a global file called GlobalAssemblyInfo.cs and a local one called AssemblyInfo.cs.我们正在使用一个名为 GlobalAssemblyInfo.cs 的全局文件和一个名为 AssemblyInfo.cs 的本地文件。 The global file contains the following attributes:全局文件包含以下属性:

 [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 Add/Existing Item...在项目的上下文菜单中
  • Select GlobalAssemblyInfo.cs 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 Select 按钮下拉列表中的“添加为链接”

In my case, we're building a product for which we have a Visual Studio solution, with various components in their own projects.在我的例子中,我们正在构建一个产品,我们有一个 Visual Studio 解决方案,在他们自己的项目中包含各种组件。 The common attributes go.共同属性 go。 In the solution, there are about 35 projects, and a common assembly info (CommonAssemblyInfo.cs), which has the following attributes:在解决方案中,大约有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.其他属性,例如 AssemblyTitle、AssemblyVersion 等,我们基于每个程序集提供。 When building an assembly both AssemblyInfo.cs and CommonAssemblyInfo.cs are built into each assembly.构建程序集时,AssemblyInfo.cs 和 CommonAssemblyInfo.cs 都内置到每个程序集中。 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.这为我们提供了两全其美的优势,您可能希望为所有项目提供一些共同属性,并为其他一些项目提供特定值。

Hope that helps.希望有帮助。

The solution presented by @JRoppert is almost the same as what I do. @JRoppert 提出的解决方案与我所做的几乎相同。 The only difference is that I put the following lines in the local AssemblyInfo.cs file as they can vary with each assembly:唯一的区别是我将以下几行放在本地 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 Explorer 显示的“ProductVersion”值。

MSBuild Community Tasks contains a custom task called AssemblyInfo which you can use to generate your assemblyinfo.cs. MSBuild 社区任务包含一个名为 AssemblyInfo 的自定义任务,您可以使用它来生成您的 assemblyinfo.cs。 It requires a little hand-editing of your csproj files to use, but is worthwhile.它需要对您的 csproj 文件进行一点手工编辑才能使用,但这是值得的。

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 (ie Company, Product etc) the changes are usually so infrequent and simple to manage I don't think DRY should be a consideration.对于全局值(即公司、产品等)的更改,这些更改通常很少且易于管理,我认为DRY不应该是一个考虑因素。 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:当您想一次性手动更改所有项目中的值时,只需运行以下 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>

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.为此,请添加一个现有文件,然后单击文件选择器中的“添加为链接”。 添加为链接
(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.诸如版权,公司,也许是版本之类的东西。

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.exe to turn a template AssemblyInfo.wcrev into AssemblyInfo.cs .我使用 TortoiseSvn,很容易使用它的SubWCRev.exe将模板AssemblyInfo.wcrev转换为AssemblyInfo.cs The relevant line in the template might look something like this:模板中的相关行可能如下所示:

[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.wcrev to your version control and ignore AssemblyInfo.cs if you use this.顺便说一句,将AssemblyInfo.wcrev添加到您的版本控制中,如果您使用它,请忽略AssemblyInfo.cs

Using a single AseemblyInfo.cs file for multiple projects is not recommended.不建议对多个项目使用单个 AseemblyInfo.cs 文件。 The AssemblyInfo file includes information that might be relevant only for that specific assembly. AssemblyInfo 文件包含可能仅与该特定程序集相关的信息。 The two most obvious pieces of information are the AssemblyTitle and AssemblyVersion .两条最明显的信息是AssemblyTitleAssemblyVersion

A better solution might be to use targets file, which are handled by the MSBuild, in order to "inject" assembly attributes to more than one project.更好的解决方案可能是使用由 MSBuild 处理的targets文件,以便将程序集属性“注入”多个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM