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,
}
///
/// 返回枚举类型的注释
///
///
/// 例:
/// public enum Days
/// {
/// [Description("小木ですよ、 -~\"#$%#%$#\"===")]
/// Sun,
/// [Description("今天星期一 猴子穿新衣")]
/// Mon,
/// [Description("今天星期二 猴子肚子餓")]
/// Tue,
/// [Description("今天星期三 猴子去爬山")]
/// Wed,
/// [Description("今天星期四 猴子去考試")]
/// Thu,
/// [Description("今天星期五 猴子去跳舞")]
/// Fri,
/// [Description("今天星期六 猴子去斗六")]
/// Sat,
/// Inter,
/// }
///
///
public static string ToDescription(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;
}
}
///
/// 任意オブジェクトのAttribute取得
///
///
///
public static string GetDisplayName(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;
}
}
///
/// 任意オブジェクトのハンドル取得
///
///
///
public static long ToHandle64(this T source) where T : class
{
try
{
return source.GetType().TypeHandle.Value.ToInt64();
}
catch
{
return 0;
}
}
///
/// Null判断
///
///
///
///
public static bool IsNull(this IDataReader dataReader, string columnName)
{
return dataReader[columnName] == DBNull.Value;
}
//public static T ToInstance(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);
// }
//}
}
}