我試圖編寫一個擴展方法可爲空的枚舉。
就像這個例子:可擴展枚舉的擴展方法
// ItemType is an enum
ItemType? item;
...
item.GetDescription();
所以我寫了這個方法,它不會編譯由於某種原因,我不明白:
public static string GetDescription(this Enum? theEnum)
{
if (theEnum == null)
return string.Empty;
return GetDescriptionAttribute(theEnum);
}
我越來越對Enum?
以下錯誤:
唯一非空值類型可以被底層的system.nullable
爲什麼?枚舉不能有值null
!
更新:
如果有大量的枚舉,ItemType
只是其中的一個例子。
http://msdn.microsoft.com/en-us/library/system.enum.aspx – Jacek
@Jacek你是什麼意思? – Rawling
@Jacek:我打算寫這篇文章,但是我測試了它,它出乎意料地工作。 – Jens