Linux 拨号vps windows公众号手机端

.net 读取项目AssemblyInfo.cs属性值

lewis 7年前 (2018-12-01) 阅读数 15 #程序编程
文章标签 .net

要在.NET中读取项目的AssemblyInfo.cs文件中的属性值,可以使用System.Reflection命名空间中的Assembly类。

以下是一个示例代码,它演示如何读取AssemblyInfo.cs文件中的AssemblyTitle属性值:

using System;
using System.Reflection;
class Program
{
static void Main()
{
// 获取当前程序集的Assembly对象
Assembly assembly = Assembly.GetExecutingAssembly();
// 获取AssemblyTitle属性值
string assemblyTitle = assembly.GetCustomAttribute<AssemblyTitleAttribute>()?.Title;
// 打印AssemblyTitle属性值
Console.WriteLine("Assembly Title: " + assemblyTitle);
}
}

上述代码首先通过Assembly.GetExecutingAssembly()方法获取当前程序集的Assembly对象。然后,它使用GetCustomAttribute<T>方法获取指定类型的自定义属性。在这里,我们使用AssemblyTitleAttribute类型来获取AssemblyTitle属性值。

请注意,GetCustomAttribute<T>方法返回的是指定类型的自定义属性的实例,因此我们还需要通过属性实例访问属性的值。在这里,我们使用Title属性来获取AssemblyTitle属性的值。

这只是一个示例,你可以根据自己的需要修改代码来读取其他属性值。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门