在典型的.NET应用程序中,产品和版本信息存储在'Properties'文件夹下的AssemblyInfo.cs
文件中,如此...
DLL Project
- Properties Folder
- AssemblyInfo.cs
在我们的例子中,我们有一个解决方案,我们需要保持版本信息在11个DLL之间同步。
为实现此目的,我们首先从每个项目的AssemblyInfo.cs
文件中删除任何共享值,只留下特定于该特定项目的那些值。
然后,我们将所有共享值放在名为AssemblyInfo_Shared.cs
的第二个文件中,该文件存储在兄弟文件夹中,用于项目的文件夹。然后我们通过链接将该文件添加到每个项目的“属性”文件夹中(当您添加它时,这是按钮上的小“向下”箭头。)
通过这样做,所有相关的DLL共享相同的版本信息,同时仍保留特定于程序集的属性。当我们编辑单个文件并且所有11个DLL的版本一次更新时,它使一组版本化的DLL同步变得轻而易举。
这是它的样子......
Common Folder
- AssemblyInfo_Shared.cs (Actual)
DLL Project A
- Properties Folder
- AssemblyInfo.cs // Only values specific to A
- AssemblyInfo_Shared.cs (Link)
DLL Project B
- Properties Folder
- AssemblyInfo.cs // Only values specific to B
- AssemblyInfo_Shared.cs (Link)
项目A中的AssemblyInfo.cs的内容如下所示......
using System.Reflection;
[assembly: AssemblyTitle("SomeApp.LibA")]
[assembly: AssemblyDescription("This is the code for A")]
这是项目B的
using System.Reflection;
[assembly: AssemblyTitle("SomeApp.LibB")]
[assembly: AssemblyDescription("This is the code for Project B")]
这是共享的......
using System;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
[assembly: AssemblyProduct("SomeApp")]
[assembly: AssemblyVersion("1.4.3.0")]
[assembly: AssemblyFileVersion("1.4.3.0")]
[assembly: AssemblyCompany("MyCo")]
[assembly: AssemblyCopyright("Copyright (c) 2010-2018, MyCo")]
[assembly: ComVisible(false)]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: CLSCompliant(true)]
有了上述内容,两个DLL共享相同的版本信息,因为公共文件在构建时与项目特定文件“合并”。希望这是有道理的。
但是,在.NET标准项目中,看起来版本信息被直接放到<PropertyGroup>
部分下的Project文件中,所以我不确定如何实现相同的功能。
.NET Standard有什么支持这个吗?
是的,你可以; o)
将名为Properties的文件夹添加到每个项目根目录,并将链接添加到AssemblyInfo_Shared.cs。
之后,您必须在每个.Net标准/核心项目配置中进行设置
<PropertyGroup>
...
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
您将在https://github.com/SirRufo/SharedVersionTest找到完整的解决方案
Rufo爵士的解决方案非常好,但这是另一种方式,通过新的项目格式实现。这使您可以自定义自动装配生成。
您可以将Directory.Build.props
文件放在项目的父目录中,它将自动包含在内。见the documentation:
但是,现在您可以通过在包含源的根文件夹中名为Directory.Build.props的单个文件中将其添加到一个步骤中,从而在一个步骤中为每个项目添加新属性。运行MSBuild时,Microsoft.Common.props会在目录结构中搜索Directory.Build.props文件(而Microsoft.Common.targets会查找Directory.Build.targets)。如果找到一个,则导入该属性。 Directory.Build.props是一个用户定义的文件,它为目录下的项目提供自定义。
所以你可以利用它来添加你的装配信息:
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>1.4.3.0</Version>
<Product>SomeApp</Product>
<Company>MyCo</Company>
<Copyright>Copyright (c) 2010-2018, MyCo</Copyright>
</PropertyGroup>
</Project>
可能没有CLSCompliant
属性的标签,但是你将拥有组装信息所需的一切,以及其他一些东西。 Here是与NuGet相关的属性的文档,但不幸的是,现在汇编信息属性的唯一“文档”是the source code here(至少this issue被解析)。
然后,您可以覆盖每个项目中所需的任何属性。