使用汇编属性的最佳实践是什么?

时间:2023-02-10 20:04:13

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?

我有一个多项目的解决方案。我正在优化汇编信息。cs文件通过链接一个解决方案广泛的汇编信息文件。这样做的最佳实践是什么?解决方案范围内的文件中应该包含哪些属性,哪些属性是特定于项目/程序集的?


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

编辑:如果你有兴趣,有一个后续问题,在汇编版本,汇编文件版本和汇编信息版本之间有什么区别?

8 个解决方案

#1


196  

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

我们正在使用一个全局文件,叫做globalassembly yinfo。还有一个叫汇编信息。全局文件包含以下属性:

 [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:

你可以添加globalassembly信息。cs使用以下程序:

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

#2


16  

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.

其他属性,如程序集标题、程序集版本等,我们在每个程序集的基础上提供。在构建程序集时,请同时使用汇编信息。cs和CommonAssemblyInfo。每个程序集都内置了cs。这为我们提供了最好的两个方面,您可能希望为所有项目拥有一些公共属性,并为其他一些项目提供特定的值。

Hope that helps.

希望有帮助。

#3


12  

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提供的解决方案与我所做的几乎相同。唯一的区别是我在本地程序集信息中加入了以下几行。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”值。

#4


8  

MSBuild Community Tasks contains 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社区任务包含一个名为assembly yinfo的自定义任务,您可以使用它来生成您的程序集info.cs。它需要手工编辑您的csproj文件来使用,但这是值得的。

#5


4  

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.

在我看来,使用全球汇编信息。cs比它的价值更麻烦,因为您需要修改每个项目文件并记住修改每个新项目,而您得到的是一个程序集信息。默认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 DRY should 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:

对于全球价值观的改变(例如,公司、产品等),变化通常是如此的不频繁和容易管理,我不认为干应该是一个考虑因素。当您想一次性手工更改所有项目中的值时,只需运行以下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>

#6


3  

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 http://laurent.etiemble.free.fr/dotclear/images/AddLinkedFile03_tn.png

为此,添加一个现有文件,并在文件选择器中单击“作为链接添加”。添加链接http://laurent.etiemble.free.fr/dotclear/images/AddLinkedFile03_tn.png

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.

至于在共享文件中放入什么,我建议将会在程序集之间共享的东西。比如版权,公司,也许是版本。

#7


1  

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 AssemblyTitle and AssemblyVersion.

使用单一AseemblyInfo。不建议多个项目使用cs文件。程序集信息文件包含的信息可能只与特定程序集相关。最明显的两个信息是程序集和程序集。

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处理的目标文件,以便将装配属性“注入”到多个项目中。

#8


1  

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

我发现一件有用的事情是通过在预构建阶段中应用符号替换来生成程序集版本元素(等等)。

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

我用玳瑁做的,它的子房很容易用。转换模板程序集。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.wcrev to your version control and ignore AssemblyInfo.cs if you use this.

顺便说一下,添加汇编信息。wcrev到您的版本控制,并忽略汇编信息。如果你用这个。

#1


196  

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

我们正在使用一个全局文件,叫做globalassembly yinfo。还有一个叫汇编信息。全局文件包含以下属性:

 [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:

你可以添加globalassembly信息。cs使用以下程序:

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

#2


16  

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.

其他属性,如程序集标题、程序集版本等,我们在每个程序集的基础上提供。在构建程序集时,请同时使用汇编信息。cs和CommonAssemblyInfo。每个程序集都内置了cs。这为我们提供了最好的两个方面,您可能希望为所有项目拥有一些公共属性,并为其他一些项目提供特定的值。

Hope that helps.

希望有帮助。

#3


12  

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提供的解决方案与我所做的几乎相同。唯一的区别是我在本地程序集信息中加入了以下几行。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”值。

#4


8  

MSBuild Community Tasks contains 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社区任务包含一个名为assembly yinfo的自定义任务,您可以使用它来生成您的程序集info.cs。它需要手工编辑您的csproj文件来使用,但这是值得的。

#5


4  

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.

在我看来,使用全球汇编信息。cs比它的价值更麻烦,因为您需要修改每个项目文件并记住修改每个新项目,而您得到的是一个程序集信息。默认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 DRY should 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:

对于全球价值观的改变(例如,公司、产品等),变化通常是如此的不频繁和容易管理,我不认为干应该是一个考虑因素。当您想一次性手工更改所有项目中的值时,只需运行以下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>

#6


3  

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 http://laurent.etiemble.free.fr/dotclear/images/AddLinkedFile03_tn.png

为此,添加一个现有文件,并在文件选择器中单击“作为链接添加”。添加链接http://laurent.etiemble.free.fr/dotclear/images/AddLinkedFile03_tn.png

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.

至于在共享文件中放入什么,我建议将会在程序集之间共享的东西。比如版权,公司,也许是版本。

#7


1  

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 AssemblyTitle and AssemblyVersion.

使用单一AseemblyInfo。不建议多个项目使用cs文件。程序集信息文件包含的信息可能只与特定程序集相关。最明显的两个信息是程序集和程序集。

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处理的目标文件,以便将装配属性“注入”到多个项目中。

#8


1  

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

我发现一件有用的事情是通过在预构建阶段中应用符号替换来生成程序集版本元素(等等)。

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

我用玳瑁做的,它的子房很容易用。转换模板程序集。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.wcrev to your version control and ignore AssemblyInfo.cs if you use this.

顺便说一下,添加汇编信息。wcrev到您的版本控制,并忽略汇编信息。如果你用这个。