要在.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()?.Title; // 打印AssemblyTitle属性值 Console.WriteLine("Assembly Title: " + assemblyTitle); } }
上述代码首先通过Assembly.GetExecutingAssembly()
方法获取当前程序集的Assembly
对象。然后,它使用GetCustomAttribute
方法获取指定类型的自定义属性。在这里,我们使用AssemblyTitleAttribute
类型来获取AssemblyTitle属性值。
请注意,GetCustomAttribute
方法返回的是指定类型的自定义属性的实例,因此我们还需要通过属性实例访问属性的值。在这里,我们使用Title
属性来获取AssemblyTitle属性的值。
这只是一个示例,你可以根据自己的需要修改代码来读取其他属性值。