using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.ComponentModel.DataAnnotations;
|
using System.Data;
|
using System.Reflection;
|
using System.Text;
|
|
namespace HotelPms.Share.Util
|
{
|
public static class ObjectEx
|
{
|
public enum Days
|
{
|
[Description("小木ですよ、 -~\"#$%#%$#\"===")]
|
Sun,
|
[Description("今天星期一 猴子穿新衣")]
|
Mon,
|
[Description("今天星期二 猴子肚子餓")]
|
Tue,
|
[Description("今天星期三 猴子去爬山")]
|
Wed,
|
[Description("今天星期四 猴子去考試")]
|
Thu,
|
[Description("今天星期五 猴子去跳舞")]
|
Fri,
|
[Description("今天星期六 猴子去斗六")]
|
Sat,
|
Inter,
|
}
|
|
/// <summary>
|
/// 返回枚举类型的注释
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// 例:
|
/// public enum Days
|
/// {
|
/// [Description("小木ですよ、 -~\"#$%#%$#\"===")]
|
/// Sun,
|
/// [Description("今天星期一 猴子穿新衣")]
|
/// Mon,
|
/// [Description("今天星期二 猴子肚子餓")]
|
/// Tue,
|
/// [Description("今天星期三 猴子去爬山")]
|
/// Wed,
|
/// [Description("今天星期四 猴子去考試")]
|
/// Thu,
|
/// [Description("今天星期五 猴子去跳舞")]
|
/// Fri,
|
/// [Description("今天星期六 猴子去斗六")]
|
/// Sat,
|
/// Inter,
|
/// }
|
/// <param name="source"></param>
|
/// <returns></returns>
|
public static string ToDescription<T>(this T source) where T : Enum
|
{
|
try
|
{
|
FieldInfo fi = source.GetType().GetField(source.ToString());
|
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
if (attributes != null && attributes.Length > 0) { return attributes[0].Description; } else { return source.ToString(); }
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
|
/// <summary>
|
/// 任意オブジェクトのAttribute取得
|
/// </summary>
|
/// <param name="source"></param>
|
/// <returns></returns>
|
public static string GetDisplayName<T>(this T source, string property) where T : class
|
{
|
try
|
{
|
PropertyInfo p = source.GetType().GetProperty(property);
|
DisplayAttribute[] attributes = (DisplayAttribute[])p.GetCustomAttributes(typeof(DisplayAttribute), false);
|
if (attributes != null && attributes.Length > 0) { return attributes[0].Name; } else { return string.Empty; }
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
|
/// <summary>
|
/// 任意オブジェクトのハンドル取得
|
/// </summary>
|
/// <param name="source"></param>
|
/// <returns></returns>
|
public static long ToHandle64<T>(this T source) where T : class
|
{
|
try
|
{
|
return source.GetType().TypeHandle.Value.ToInt64();
|
}
|
catch
|
{
|
return 0;
|
}
|
}
|
|
/// <summary>
|
/// Null判断
|
/// </summary>
|
/// <param name="dataReader"></param>
|
/// <param name="columnName"></param>
|
/// <returns></returns>
|
public static bool IsNull(this IDataReader dataReader, string columnName)
|
{
|
return dataReader[columnName] == DBNull.Value;
|
}
|
|
//public static T ToInstance<T>(long handle) where T : class
|
//{
|
// try
|
// {
|
// unsafe
|
// {
|
// IntPtr ptr = new IntPtr(handle);
|
// Object obj = Pointer.Box((void*)ptr, typeof(T));
|
// return obj as T;
|
// }
|
// }
|
// catch
|
// {
|
// return default(T);
|
// }
|
//}
|
}
|
}
|