当前位置: 移动技术网 > IT编程>开发语言>.net > .net 读取项目AssemblyInfo.cs属性值

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

ico是什么,96s406,刘嘉玲丰6

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网