c# AssemblyInfo 解释 .net 读取项目AssemblyInfo.cs属性值

本文介绍如何在C#项目中利用AssemblyInfo.cs文件获取项目元数据,并展示了如何通过反射读取这些元数据用于显示关于对话框。具体包括:项目名称、版权、描述、产品信息、版本号等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用VS创建C#项目(File->New->Project->Visual C#->Class Library),它会自动为我们产生一个AssemblyInfo.cs文件。在一个实际的项目当中,我们必须修改该文件的内容。How to use the following info: 
AssemblyInfo ainfo = new AssemblyInfo(); 
frmAbout.Text = ainfo.Title; 
frmAbout.menuAbt.Text = string.Format("&About{0}..",ainfo.Title); 
frmAbout.Text = "About " + this.Owner.Text; 
frmAbout.Icon = this.Owner.Icon; 
//You can set the icon like this on the abt form. 
frmAbout.pictureBox1.Image = this.Owner.Icon.ToBitmap(); 
frmAbout.lblTitle.Text = ainfo.Title; 
frmAbout.lblVersion.Text = ainfo.Version; 
frmAbout.lblCopyright.Text = ainfo.Copyright; 
frmAbout.lblDescription.Text = ainfo.Description; 
frmAbout.lblCodebase.Text = ainfo.CodeBase; 
下面是具体的实现代码。
using System; 
using System.Reflection; 
using System.Runtime.CompilerServices; 
[assembly: AssemblyTitle("Demo Title")] 
[assembly: AssemblyDescription("Demo app that reads from the Assembly Info file description")] 
[assembly: AssemblyConfiguration("")] 
[assembly: AssemblyCompany("World Company")] 
[assembly: AssemblyProduct("Not for commercial use.")] 
[assembly: AssemblyCopyright("open source (US)")] 
[assembly: AssemblyTrademark("")] 
[assembly: AssemblyCulture("")] 
[assembly: CLSCompliant(true)] 
[assembly: AssemblyDelaySign(false)] 
[assembly: AssemblyKeyFile("")] 
[assembly: AssemblyKeyName("")] 
// 
// Version information for an assembly consists of the following four values: 
// 
// Major Version 
// Minor Version 
// Build Number 
// Revision 
// 
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below: 
[assembly: AssemblyVersion("1.0.1.1")] 
# region "Class to get the information for AboutForm" 
/* This class uses the System.Reflection.Assembly class to access assembly meta-data. 
* This class is not a normal feature of AssmblyInfo.cs */ 
/// <summary> 
/// AssemblyInfo class. 
/// </summary> 
public class AssemblyInfo 

//Used by functions to access information from Assembly Attributes 
/// <summary> 
/// myType. 
/// </summary> 
private Type myType; 
/// <summary> 
/// Initializes a new instance of the <see cref="AssemblyInfo"/> class. 
/// </summary> 
public AssemblyInfo() 

//Shellform here denotes the actual form. 
myType = typeof(ShellForm); 

/// <summary> 
/// Gets the name of the assembly. 
/// </summary> 
/// <value>The name of the assembly.</value> 
public String AssemblyName 

get 

return myType.Assembly.GetName().Name.ToString(); 


/// <summary> 
/// Gets the full name of the assembly. 
/// </summary> 
/// <value>The full name of the assembly.</value> 
public String AssemblyFullName 

get 

return myType.Assembly.GetName().FullName.ToString(); 


/// <summary> 
/// Gets the code base. 
/// </summary> 
/// <value>The code base.</value> 
public String CodeBase 

get 

return myType.Assembly.CodeBase; 


/// <summary> 
/// Gets the copyright. 
/// </summary> 
/// <value>The copyright.</value> 
public String Copyright 

get 

Type att = typeof(AssemblyCopyrightAttribute); 
object[] r = myType.Assembly.GetCustomAttributes(att, false); 
AssemblyCopyrightAttribute copyattr = (AssemblyCopyrightAttribute)r[0]; 
return copyattr.Copyright; 


/// <summary> 
/// Gets the company. 
/// </summary> 
/// <value>The company.</value> 
public String Company 

get 

Type att = typeof(AssemblyCompanyAttribute); 
object[] r = myType.Assembly.GetCustomAttributes(att, false); 
AssemblyCompanyAttribute compattr = (AssemblyCompanyAttribute)r[0]; 
return compattr.Company; 


/// <summary> 
/// Gets the description. 
/// </summary> 
/// <value>The description.</value> 
public String Description 

get 

Type att = typeof(AssemblyDescriptionAttribute); 
object[] r = myType.Assembly.GetCustomAttributes(att, false); 
AssemblyDescriptionAttribute descattr = (AssemblyDescriptionAttribute)r[0]; 
return descattr.Description; 


/// <summary> 
/// Gets the product. 
/// </summary> 
/// <value>The product.</value> 
public String Product 

get 

Type att = typeof(AssemblyProductAttribute); 
object[] r = myType.Assembly.GetCustomAttributes(att, false); 
AssemblyProductAttribute prodattr = (AssemblyProductAttribute)r[0]; 
return prodattr.Product; 


/// <summary> 
/// Gets the title. 
/// </summary> 
/// <value>The title.</value> 
public String Title 

get 

Type att = typeof(AssemblyTitleAttribute); 
object[] r = myType.Assembly.GetCustomAttributes(att, false); 
AssemblyTitleAttribute titleattr = (AssemblyTitleAttribute)r[0]; 
return titleattr.Title; 


/// <summary> 
/// Gets the version. 
/// </summary> 
/// <value>The version.</value> 
public String Version 

get 

return myType.Assembly.GetName().Version.ToString(); 



# endregion

转载于:https://www.cnblogs.com/dowms22/archive/2010/01/08/1642261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值