从 .NET Core 中的 C# 访问“.csproj”属性

gal*_*h92 4 csproj .net-core

我的.csproj文件包含:

...
<PackageId>MyProject</PackageId>
<Version>1.0.0</Version>
...
Run Code Online (Sandbox Code Playgroud)

我如何从我的项目代码中访问它?

koe*_*oof 7

要访问项目的版本和更常规的属性,请使用: GetCustomAttribute<T>()

用作AssemblyInformationalVersionAttribute类型 T 来检索 Version 属性。

有关 T 的其他值,请参阅https://learn.microsoft.com/en-us/dotnet/api/system.reflection?view=netcore-2.0上列出的属性

例子: typeof(Startup).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion