c# - .NET Core - 在 C# 代码中获取项目描述

标签 c# .net .net-core

我目前正在开发一个 .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/

相关文章:

c# - SharePoint 列值包含额外的 ;# 字符

c# - 创建新字段的快捷方式

.net - 如何在 .NET 类的方法和属性中注入(inject)管道代码?

c# - 如何访问匿名对象的属性

sql-server - EF Core Update-Database 命令为用户创建架构而不是使用 dbo

c# - 网络核心/身份服务器: unable to logout

c# - LINQ to SQL 十进制参数

c# - 从 Salesforce Apex 标注捕获/查看 http POST

c# - Serilog:如何进一步增强 appsettings.json 中的过滤器表达式?

c# - 自定义引用循环处理