.NET Core-使用C#代码获取项目描述

安都因人

我目前正在从事.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>

如您所见,值中包含一个Description字符串PropertyGroup

这是项目说明

我需要在运行时使用C#代码获取此值。喜欢:

public static void Main(string[] args)
{
    string projectDescription = ??? // How can I get: "This is the project description"
    Console.WriteLine(projectDescription );
}
萨耶塔兰(Sajeetharan)

您可以使用以下命令访问项目的版本和更多常规属性:

GetCustomAttribute<T>()

有关T的其他值,请参见https://docs.microsoft.com/zh-cn/dotnet/api/system.reflection?view=netcore-2.0上列出的属性。

例:

using System.Reflection;

var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章