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

转载 2012年03月31日 11:57:08

.net 读取项目AssemblyInfo.cs属性值的实现代码。

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. 
/// </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
详细出处参考:http://www.jb51.net/article/18650.htm


详细出处参考:http://www.jb51.net/article/18650.htm

 

读取项目AssemblyInfo.cs属性值

Assembly ae = Assembly.GetExecutingAssembly();object[] obj = ae.GetCustomAttributes(typeof (Assembly...
  • jlxyfeng
  • jlxyfeng
  • 2010年11月27日 14:03
  • 464

C#取得AssemblyInfo.cs的项目属性

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy...
  • firetaker
  • firetaker
  • 2010年04月30日 08:46
  • 6881

VS中实时获取SVN的版本号并写入到AssemblyInfo.cs中(C#)

在开发项目时,需要知道当前发布的到底是哪个版本,比较好的方式就是获取SVN的版本来作为项目的版本。项目版本一般由主版本、次版本、内部版本、修改版本四个部分组成,我们获取的SVN版本就作为修改版本即可。...
  • xxdddail
  • xxdddail
  • 2015年10月20日 17:57
  • 2928

C# AssemblyInfo.cs文件的作用

C# AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数请看以下具体说明://标题:  [assembly:AssemblyTitle("编程论坛")]//备注:...
  • zmhdgut
  • zmhdgut
  • 2008年07月12日 12:48
  • 3722

.Net魔法堂:AssemblyInfo.cs文件详解

.net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件。但我们实际上通过另一个形式操作该文件。那就是通过在鼠标右键点击项目的属...
  • linshichen
  • linshichen
  • 2016年06月29日 14:44
  • 397

asp.net获取xml属性的值两种方法

private string GetAttributename(string filename, string selNodeName, string attributename) {...
  • happy664618843
  • happy664618843
  • 2010年09月21日 16:51
  • 2491

xml 读取节点中指定的属性值

例子: 一个xml文件:
  • jixiaomeng821
  • jixiaomeng821
  • 2014年05月18日 11:28
  • 14176

Java读取XML文件(2)读取属性

Java读取XML文件(2)读取属性 读取属性  ReadXml.java 。。。 public static void showElem(NodeList nl) { ...
  • C_Major
  • C_Major
  • 2015年05月05日 11:34
  • 1681

C#利用反射获取对象属性值

C#利用反射获取对象属性值   public static string GetObjectPropertyValueT>(T t, string propertyname){     ...
  • liuruxin
  • liuruxin
  • 2014年09月10日 11:31
  • 13749

ASP.NET - 查询邮件及邮件内容

挺早之前整理的一套邮件信息查询功能。 后半部分贴入完整前后台代码。  首先需要添加引用OpenPop.NET类,版本为2.0.5: 已存入百度云 共分为两个页面:1.邮件列表页面,不包含邮件内容  ...
  • xianglikai1
  • xianglikai1
  • 2017年08月25日 11:37
  • 750
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:.net 读取项目AssemblyInfo.cs属性值
举报原因:
原因补充:

(最多只允许输入30个字)