从 .NET Core 中的 C# 访问“.csproj”属性
Access `.csproj` attributes from C# in .NET Core
我的 .csproj
文件包含:
...
<PackageId>MyProject</PackageId>
<Version>1.0.0</Version>
...
如何从我的项目代码中访问它?
要访问项目的版本和更多常规属性,请使用:
GetCustomAttribute<T>()
使用 AssemblyInformationalVersionAttribute
作为类型 T 检索版本 属性。
有关 T 的其他值,请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.reflection?view=netcore-2.0
中列出的属性
示例:
typeof(Startup).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion
我的 .csproj
文件包含:
...
<PackageId>MyProject</PackageId>
<Version>1.0.0</Version>
...
如何从我的项目代码中访问它?
要访问项目的版本和更多常规属性,请使用:
GetCustomAttribute<T>()
使用 AssemblyInformationalVersionAttribute
作为类型 T 检索版本 属性。
有关 T 的其他值,请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.reflection?view=netcore-2.0
中列出的属性示例:
typeof(Startup).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion