CodeProject上很有趣很經典的Enum寫法,非常的實用!
1.
定義enum field可用的Attribute
/// <summary>
/// Attribute for Enum class
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class EnumAttr: Attribute
{
/// <summary>
/// Description of the enum field
/// </summary>
public String Desc { get; set; }
public EnumAttr()
{
}
}
/// <summary>
/// EnumExtension for enum fields
/// </summary>
/// <remarks>Define the following extension method to gain access to the attribute object associated with an enum field</remarks>
/// <see cref="http://www.codeproject.com/Articles/38666/Enum-Pattern"/>
public static class EnumExtension
{
public static EnumAttr GetAttr(this Enum value)
{
Type type = value.GetType();
FieldInfo fieldInfo = type.GetField(value.ToString());
var atts = (EnumAttr[])fieldInfo.GetCustomAttributes(typeof(EnumAttr), false);
return atts.Length > 0 ? atts[0] : null;
}
}
/// <summary>
/// Attribute for Enum class
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class EnumAttr: Attribute
{
/// <summary>
/// Description of the enum field
/// </summary>
public String Desc { get; set; }
public EnumAttr()
{
}
}
/// <summary>
/// EnumExtension for enum fields
/// </summary>
/// <remarks>Define the following extension method to gain access to the attribute object associated with an enum field</remarks>
/// <see cref="http://www.codeproject.com/Articles/38666/Enum-Pattern"/>
public static class EnumExtension
{
public static EnumAttr GetAttr(this Enum value)
{
Type type = value.GetType();
FieldInfo fieldInfo = type.GetField(value.ToString());
var atts = (EnumAttr[])fieldInfo.GetCustomAttributes(typeof(EnumAttr), false);
return atts.Length > 0 ? atts[0] : null;
}
}
2.
實做一個Enum 類別,在每個Enum field就可以加上EnumAttr這個屬性。
public enum ErrMsgClass
{
[EnumAttr(Desc="尚未定義")]
NotDefined,
[EnumAttr(Desc = "尚未實作")]
NotImplement,
[EnumAttr(Desc = "溢位")]
Overflow,
[EnumAttr(Desc = "物件為空值")]
NullObject
}
public enum ErrMsgClass
{
[EnumAttr(Desc="尚未定義")]
NotDefined,
[EnumAttr(Desc = "尚未實作")]
NotImplement,
[EnumAttr(Desc = "溢位")]
Overflow,
[EnumAttr(Desc = "物件為空值")]
NullObject
}
3.
主程式:
String alertMsg = String.Format("此事件{0}", ErrMsgClass.NotDefined.GetAttr().Desc);
throw new NotImplementedException(alertMsg);
結果:
System.NotImplementedException : 此事件尚未定義
String alertMsg = String.Format("此事件{0}", ErrMsgClass.NotDefined.GetAttr().Desc);
throw new NotImplementedException(alertMsg);
結果:
System.NotImplementedException : 此事件尚未定義
4.
備註:
也可以定義一些新的屬性欄位在繼承EnumAttr的子類別,例如 …
也可以定義一些新的屬性欄位在繼承EnumAttr的子類別,例如 …
u 增加一個繼承EnumAttr的子類別
public class ErrMsgClassAttr : EnumAttr
{
public int Level { get; set; }
public String EngDesc { get; set; }
}
public class ErrMsgClassAttr : EnumAttr
{
public int Level { get; set; }
public String EngDesc { get; set; }
}
u 修改原有的Enum class,將Attribute從EnumAttr改為ErrMsgClassAttr
public enum ErrMsgClass
{
[ErrMsgClassAttr(Desc = "尚未定義", EngDesc = "Not defined", Level = 3)]
NotDefined,
[ErrMsgClassAttr(Desc = "尚未定義", EngDesc = "Not defined", Level = 3)]
NotImplement,
//…
}
public enum ErrMsgClass
{
[ErrMsgClassAttr(Desc = "尚未定義", EngDesc = "Not defined", Level = 3)]
NotDefined,
[ErrMsgClassAttr(Desc = "尚未定義", EngDesc = "Not defined", Level = 3)]
NotImplement,
//…
}
u 主程式:
請注意要將Enum的GetAttr()轉型為ErrMsgClassAttr為,才能取得Level和EngDesc兩個子類別的屬性欄位。
ErrMsgClassAttr attr = ErrMsgClass.NotImplement.GetAttr() as ErrMsgClassAttr;
String alertMsg = String.Format(
"Level{0} : DoInsert event {1}, ",
attr.Level.ToString(),
attr.EngDesc);
throw new NotImplementedException(alertMsg);
結果:
System.NotImplementedException: Level3 : DoInsert event Not defined
請注意要將Enum的GetAttr()轉型為ErrMsgClassAttr為,才能取得Level和EngDesc兩個子類別的屬性欄位。
ErrMsgClassAttr attr = ErrMsgClass.NotImplement.GetAttr() as ErrMsgClassAttr;
String alertMsg = String.Format(
"Level{0} : DoInsert event {1}, ",
attr.Level.ToString(),
attr.EngDesc);
throw new NotImplementedException(alertMsg);
結果:
System.NotImplementedException: Level3 : DoInsert event Not defined
5.
結束。
沒有留言:
張貼留言