以上技术均来自互联网,由笔者完善,改装,直接可以使用的。并且进行扩展。
/*
* 概要:VersionInfo 获得版本程序集等相关信息的类。
* 描述:包括获得程序集信息
* 文件版本信息,最后访问日期,通过FileInfo.属性访问自己转换时间格式。
* 放置到对应的程序集即可
* 版本:1.0.0.1
* 日期:
*/
//===========================================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace Foundation.Utils
{
public class VersionInfo
{
#region Assembly特性信息
public static string AssemblyTitle
{
get
{
// Get all Title attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
// If there is at least one Title attribute
if (attributes.Length > 0)
{
// Select the first one
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
// If it is not an empty string, return it
if (!string.IsNullOrEmpty(titleAttribute.Title))
return titleAttribute.Title;
}
// If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}
public static string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public static string AssemblyDescription
{
get
{
// Get all Description attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
// If there aren't any Description attributes, return an empty string
if (attributes.Length == 0)
return "";
// If there is a Description attribute, return its value
return ((AssemblyDescriptionAttribute)attributes[0]).Description;
}
}
public static string AssemblyProduct
{
get
{
// Get all Product attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
// If there aren't any Product attributes, return an empty string
if (attributes.Length == 0)
return "";
// If there is a Product attribute, return its value
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}
public static string AssemblyCopyright
{
get
{
// Get all Copyright attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
// If there aren't any Copyright attributes, return an empty string
if (attributes.Length == 0)
return "";
// If there is a Copyright attribute, return its value
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}
public static string AssemblyCompany
{
get
{
// Get all Company attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
// If there aren't any Company attributes, return an empty string
if (attributes.Length == 0)
return "";
// If there is a Company attribute, return its value
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
#region File文件特性信息
//文件名
public static string FileName
{
get
{
return System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
}
}
//文件信息(可以获得最后修改时间)
private static FileInfo _fileInfo;
public static FileInfo FileInfo
{
get
{
if (_fileInfo ==null)
_fileInfo = new FileInfo(FileName);
return _fileInfo;
}
}
//文件版本信息
private static FileVersionInfo _fileVersionInfo;
public static FileVersionInfo FileVersionInfo
{
get
{
if (_fileVersionInfo == null)
_fileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(FileName);
return _fileVersionInfo;
}
}
public static string LastModifyTime
{
get
{
return FileInfo.LastAccessTime.ToString("yyyy年MM月dd日");
}
}
#endregion
}
}
/**********************************************************************************
* 概要: IListSort<T>
* 描述: IList排序类
* 版本: 暂时使用插入排序方法
* 技术来源于互联网
* 使用方法:
* 多字段排序:
* //调用方法
IList<class> List = new List<class>();
//排序字段
string[] property = new string[] { "column1","column2" };
//对应排序字段的排序方式,默认为ture 为升序排列
bool[] sortby =new bool[]{ false,false };
//对 List 排序
List = new IListSort<class>(List, property,sortby).Sort();
*
* 单字段排序:
* IListXXX= new IListSort<MVote>(list, "SortOrder").Sort();
* 未实现扩展自定义委托。
* ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Foundation.Utils
{
/// <summary>
/// IList排序类
/// </summary>
/// <typeparam name="T"></typeparam>
public class IListSort<T>
{
private string[] _propertyName;
private bool[] _sortBy;
private IList<T> _list;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list">排序的Ilist</param>
/// <param name="propertyName">排序字段属性名</param>
/// <param name="sortBy">true升序 false 降序 不指定则为true</param>
public IListSort(IList<T> list, string[] propertyName, bool[] sortBy)
{
_list = list;
_propertyName = propertyName;
_sortBy = sortBy;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list">排序的Ilist</param>
/// <param name="propertyName">排序字段属性名</param>
/// <param name="sortBy">true升序 false 降序 不指定则为true</param>
public IListSort(IList<T> list, string[] propertyName)
{
_list = list;
_propertyName = propertyName;
for (int i = 0; i < _propertyName.Length; i++)
{
_sortBy[i] = true;
}
}
/// <summary>
/// 构造函数:重构
/// </summary>
/// <param name="list"></param>
/// <param name="propertyName"></param>
public IListSort(IList<T> list, string propertyName)
{
_list = list;
_propertyName = new string[] { propertyName };
_sortBy = new bool[] { true };
}
/// <summary>
/// 构造函数:重构
/// </summary>
/// <param name="list"></param>
/// <param name="propertyName"></param>
/// <param name="sortBy"></param>
public IListSort(IList<T> list, string propertyName, bool sortBy)
{
_list = list;
_propertyName = new string[] { propertyName };
_sortBy = new bool[] { sortBy };
}
/// <summary>
/// IList
/// </summary>
public IList<T> List
{
get { return _list; }
set { _list = value; }
}
/// <summary>
/// 排序字段属性名
/// </summary>
public string[] PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
/// <summary>
/// true升序 false 降序
/// </summary>
public bool[] SortBy
{
get { return _sortBy; }
set { _sortBy = value; }
}
/// <summary>
/// 排序,插入排序方法
/// </summary>
/// <returns></returns>
public IList<T> Sort()
{
if (_list.Count == 0) return _list;
for (int i = 1; i < _list.Count; i++)
{
T t = _list[i];
int j = i;
while ((j > 0) && Compare(_list[j - 1], t) < 0)
{
_list[j] = _list[j - 1];
--j;
}
_list[j] = t;
}
return _list;
}
/// <summary>
/// 比较大小 返回值 小于零则X小于Y,等于零则X等于Y,大于零则X大于Y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private int Compare(T x, T y)
{
int i = 0;
//检查属性名
for (i = 0; i < _propertyName.Length; ++i)
{
if (string.IsNullOrEmpty(_propertyName[i])) throw new ArgumentNullException("没有指字对象的排序字段属性名!");
}
//取属性的属性
PropertyInfo[] property = new PropertyInfo[_propertyName.Length];
for (i = 0; i < _propertyName.Length; ++i)
{
property[i] = typeof(T).GetProperty(_propertyName[i]);
if (property[i] == null) throw new ArgumentNullException("在对象中没有找到指定属性!");
}
int compare = 0;
for (i = 0; i < _propertyName.Length; ++i)
{
compare = CompareOne(x, y, property[i], _sortBy[i]);
if (compare != 0) return compare;
}
return compare;
}
private int CompareOne(T x, T y, PropertyInfo property, bool sortBy)
{
switch (property.PropertyType.ToString())
{
case "System.Int32"://整形比较
int int1 = 0;
int int2 = 0;
if (property.GetValue(x, null) != null)
{
int1 = Convert.ToInt32(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
int2 = Convert.ToInt32(property.GetValue(y, null));
}
if (sortBy)
{
return int2.CompareTo(int1);
}
else
{
return int1.CompareTo(int2);
}
break;
case "System.Double"://浮点比较
double double1 = 0;
double double2 = 0;
if (property.GetValue(x, null) != null)
{
double1 = Convert.ToDouble(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
double2 = Convert.ToDouble(property.GetValue(y, null));
}
if (sortBy)
{
return double2.CompareTo(double1);
}
else
{
return double1.CompareTo(double2);
}
break;
case "System.String"://字符串比较
string string1 = string.Empty;
string string2 = string.Empty;
if (property.GetValue(x, null) != null)
{
string1 = property.GetValue(x, null).ToString();
}
if (property.GetValue(y, null) != null)
{
string2 = property.GetValue(y, null).ToString();
}
if (sortBy)
{
return string2.CompareTo(string1);
}
else
{
return string1.CompareTo(string2);
}
break;
case "System.DateTime"://事件
DateTime DateTime1 = DateTime.Now;
DateTime DateTime2 = DateTime.Now;
if (property.GetValue(x, null) != null)
{
DateTime1 = Convert.ToDateTime(property.GetValue(x, null));
}
if (property.GetValue(y, null) != null)
{
DateTime2 = Convert.ToDateTime(property.GetValue(y, null));
}
if (sortBy)
{
return DateTime2.CompareTo(DateTime1);
}
else
{
return DateTime1.CompareTo(DateTime2);
}
break;
}
return 0;
}
}
}