栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.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"

///
/// AssemblyInfo class.
///

public class AssemblyInfo
{
//Used by functions to access information from Assembly Attributes
///
/// myType.
///

private Type myType;
///
/// Initializes a new instance of the class.
///

public AssemblyInfo()
{
//Shellform here denotes the actual form.
myType = typeof(ShellForm);
}
///
/// Gets the name of the assembly.
///

/// The name of the assembly.
public String AssemblyName
{
get
{
return myType.Assembly.GetName().Name.ToString();
}
}
///
/// Gets the full name of the assembly.
///

/// The full name of the assembly.
public String AssemblyFullName
{
get
{
return myType.Assembly.GetName().FullName.ToString();
}
}
///
/// Gets the code base.
///

/// The code base.
public String Codebase
{
get
{
return myType.Assembly.Codebase;
}
}
///
/// Gets the copyright.
///

/// The copyright.
public String Copyright
{
get
{
Type att = typeof(AssemblyCopyrightAttribute);
object[] r = myType.Assembly.GetCustomAttributes(att, false);
AssemblyCopyrightAttribute copyattr = (AssemblyCopyrightAttribute)r[0];
return copyattr.Copyright;
}
}
///
/// Gets the company.
///

/// The company.
public String Company
{
get
{
Type att = typeof(AssemblyCompanyAttribute);
object[] r = myType.Assembly.GetCustomAttributes(att, false);
AssemblyCompanyAttribute compattr = (AssemblyCompanyAttribute)r[0];
return compattr.Company;
}
}
///
/// Gets the description.
///

/// The description.
public String Description
{
get
{
Type att = typeof(AssemblyDescriptionAttribute);
object[] r = myType.Assembly.GetCustomAttributes(att, false);
AssemblyDescriptionAttribute descattr = (AssemblyDescriptionAttribute)r[0];
return descattr.Description;
}
}
///
/// Gets the product.
///

/// The product.
public String Product
{
get
{
Type att = typeof(AssemblyProductAttribute);
object[] r = myType.Assembly.GetCustomAttributes(att, false);
AssemblyProductAttribute prodattr = (AssemblyProductAttribute)r[0];
return prodattr.Product;
}
}
///
/// Gets the title.
///

/// The title.
public String Title
{
get
{
Type att = typeof(AssemblyTitleAttribute);
object[] r = myType.Assembly.GetCustomAttributes(att, false);
AssemblyTitleAttribute titleattr = (AssemblyTitleAttribute)r[0];
return titleattr.Title;
}
}
///
/// Gets the version.
///

/// The version.
public String Version
{
get
{
return myType.Assembly.GetName().Version.ToString();
}
}
}
# endregion
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/59227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号