关键代码:
usingSystem;usingSystem.IO;usingSystem.Reflection;namespaceYanZhiwei.DotNet2.Utilities.Common
{/// <summary>/// Assembly 帮助类/// </summary>publicclassAssemblyHelper
{#region构造函数
Assembly assembly =null;/// <summary>/// 构造函数/// </summary>publicAssemblyHelper()
{
assembly = Assembly.GetExecutingAssembly();
}/// <summary>/// 构造函数/// </summary>/// <param name="path">dll,exe路径</param>publicAssemblyHelper(stringpath)
{if(File.Exists(path))
assembly = Assembly.LoadFile(path);
}#endregion#region私有方法PRivatevoidGetAssemblyCommon<T>(Action<T> assemblyFacotry)whereT : Attribute
{if(assembly !=null)
{object[] _attributes = assembly.GetCustomAttributes(typeof(T),false);if(_attributes.Length > 0)
{
T _attribute = (T)_attributes[0];
assemblyFacotry(_attribute);
}
}
}#endregion#region获取应用程序集的标题/// <summary>/// 获取应用程序集的标题/// </summary>/// <returns>程序集的标题</returns>publicstringGetTitle()
{string_title =string.Empty;
GetAssemblyCommon<AssemblyTitleAttribute>(_ass => _title = _ass.Title);if(string.IsNullOrEmpty(_title))
{
_title = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}return_title;
}#endregion#region获取应用程序产品名称/// <summary>/// 获取应用程序产品名称/// </summary>/// <returns>产品名称</returns>publicstringGetProductName()
{string_product =string.Empty;
GetAssemblyCommon<AssemblyProductAttribute>(_ass => _product = _ass.Product);return_product;
}#endregion#region获取应用程序版本/// <summary>/// 获取应用程序版本/// </summary>/// <returns>版本</returns>publicstringGetVersion()
{returnassembly.GetName().Version.ToString();
}#endregion#region获取应用程序说明/// <summary>/// 获取应用程序说明/// </summary>/// <returns>说明</returns>publicstringGetDescription()
{string_description =string.Empty;
GetAssemblyCommon<AssemblyDescriptionAttribute>(_ass => _description = _ass.Description);return_description;
}#endregion#region获取应用程序版权信息/// <summary>/// 获取应用程序版权信息/// </summary>/// <returns>版权信息</returns>publicstringGetCopyright()
{string_copyright =string.Empty;
GetAssemblyCommon<AssemblyCopyrightAttribute>(_ass => _copyright = _ass.Copyright);return_copyright;
}#endregion#region获取应用程序公司名称/// <summary>/// 获取应用程序公司名称/// </summary>/// <returns>公司名称</returns>publicstringGetCompany()
{string_company =string.Empty;
GetAssemblyCommon<AssemblyCompanyAttribute>(_ass => _company = _ass.Company);return_company;
}#endregion#region获取应用程序显示名称/// <summary>/// 获取应用程序显示名称/// </summary>/// <returns>应用程序显示名称</returns>publicstringGetAppFullName()
{returnassembly.FullName.ToString();
}#endregion}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp{ background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
单元测试:
usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;namespaceYanZhiwei.DotNet2.Utilities.Common.Tests
{
[TestClass()]publicclassAssemblyHelperTests
{
AssemblyHelper assHelper =null;
[TestInitialize]publicvoidTestInit()
{string_path =string.Format(@"{0}\TestSource\DotNet2.Interfaces.dll", AppDomain.CurrentDomain.BaseDirectory);
assHelper =newAssemblyHelper(_path);
}
[TestMethod()]publicvoidGetTitleTest()
{
Assert.AreEqual("YanZhiwei.DotNet2.Interfaces", assHelper.GetTitle());
}
[TestMethod()]publicvoidGetProductNameTest()
{
Assert.AreEqual("YanZhiwei.DotNet2.Interfaces", assHelper.GetProductName());
}
[TestMethod()]publicvoidGetVersionTest()
{
Assert.AreEqual("1.0.5483.24630", assHelper.GetVersion());
}
[TestMethod()]publicvoidGetDescriptionTest()
{
Assert.AreEqual("", assHelper.GetDescription());
}
[TestMethod()]publicvoidGetCopyrightTest()
{
Assert.AreEqual("Copyright © YanZhiwei 2014", assHelper.GetCopyright());
}
[TestMethod()]publicvoidGetCompanyTest()
{
Assert.AreEqual("", assHelper.GetCompany());
}
[TestMethod()]publicvoidGetAppFullNameTest()
{
Assert.AreEqual("DotNet2.Interfaces, Version=1.0.5483.24630, Culture=neutral, PublicKeyToken=null", assHelper.GetAppFullName());
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }测试结果: