首页 > 开发 > .Net > 正文

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

2020-04-24 22:09:59
字体:
来源:转载
供稿:网友
We write all those code repetitively for dynamic assembly loading and checking to verify few properties on assemblies. It would be a great stop to write all such things in the assemblyinfo.cs (because it needs to completely describe the assembly that it is intended to serve for) You can define your About form of the application entirely using the 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.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表