我目前正在开发一个 .NET Core 项目。
这是我的 csproj
文件的一部分:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Description>This is the project description.</Description>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="SomeProject.csproj" />
</ItemGroup>
</Project>
如您所见,PropertyGroup
值中有一个 Description
字符串:
This is the project description
我需要在运行时用 C# 代码获取该值。喜欢:
public static void Main(string[] args)
{
string projectDescription = ??? // How can I get: "This is the project description"
Console.WriteLine(projectDescription );
}
最佳答案
您可以使用以下方式访问项目的版本和更常规的属性:
GetCustomAttribute<T>()
有关 T 的其他值,请参阅 https://learn.microsoft.com/en-us/dotnet/api/system.reflection?view=netcore-2.0 上列出的属性
示例:
using System.Reflection;
var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
关于c# - .NET Core - 在 C# 代码中获取项目描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58255335/