在.fsproj中为.NET核心

时间:2017-10-06 15:18:38

标签: f# .net-core

我正在尝试在.NET核心(2.0)控制台应用程序中指定程序集版本,以便我可以通过以下方式以编程方式访问它:

open System.Reflection

let main argv =
    printfn "Assembly Version is: %s" <| Assembly.GetEntryAssembly().GetName().Version.ToString()
    0

将版本字段添加到我的.fsproj文件的属性组中,例如:

 <PropertyGroup>
   <OutputType>Exe</OutputType>
   <TargetFramework>netcoreapp2.0</TargetFramework>
   <Version>1.0.0.1</Version>
 </PropertyGroup>

不会更改我的测试应用程序打印的版本(它保持在0.0.0.0)。

添加AssemblyInfo.fs文件在哪里设置AssemblyVersion属性是有效的,但是如果可能的话我想避免这种情况并使用.fsproj文件。这可能吗?

我也很高兴能够找到一个指针,指向我一般可以找到关于.fsproj的文档。

2 个答案:

答案 0 :(得分:5)

.fsproj个文件的文档

就MSBuild而言,我认为格式与csproj相同

您可以在此处找到有关csproj文件的官方文档:
https://docs.microsoft.com/en-us/dotnet/core/tools/csproj

程序集版本号

要获得程序集的“版本”,您应该注意有几种类型的版本:

  • AssemblyVersion
    major.minor.build.revision格式的数字值(例如,2.4.0.0)。公共语言运行库使用此值在强名称程序集中执行绑定操作 注意:如果AssemblyInformationalVersionAttribute属性未应用于程序集,则AssemblyVersionAttribute使用Application.ProductVersion属性指定的版本号,{{1} }和Application.UserAppDataPath属性。

  • AssemblyFileVersion
    指定Win32文件版本号的字符串值。这通常默认为装配版本。

  • AssemblyInformationalVersion
    指定公共语言运行库未使用的版本信息的字符串值,例如完整的产品版本号 注意:如果将此属性应用于程序集,则可以在运行时使用Application.UserAppDataRegistry属性获取指定的字符串。该字符串还用于Application.ProductVersionApplication.UserAppDataPath属性提供的路径和注册表项。

  • Application.ProductVersion
    定义程序集清单的其他版本信息。

您可以在official Microsoft Docs here上详细了解每一项内容 - 或者您可以阅读assemblies in general here

从引用的程序集中获取不同的版本

Application.UserAppDataRegistry

请注意,上面的代码段取自an answer to a similar SO question

直接解析// assembly version Assembly.GetExecutingAssembly().GetName().Version.ToString(); // assembly version - by path Assembly.LoadFile('your assembly file').GetName().Version.ToString(); // file version **this is likely what you are seeking** FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; // product version FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion; 文件

还可以选择使用XML解析.fsproj文件。此选项用于以编程方式添加引用或仅检查文件 - 因此它可能不适用于您的问题,但它是为了完整答案。

fsproj

答案 1 :(得分:3)

这里有一个问题:

Generate F# assemblyinfo in new fsproj

简而言之,它应该有效,但现在还不行。