我想將擴展方法添加到枚舉類型,但下面的代碼失敗。編譯器給出的錯誤行StoreType.GetAllItems
如何將擴展方法添加到枚舉類型?擴展和枚舉類型
namespace ConsoleApplication1
{
public static class EnumExtensions
{
public static IEnumerable<T> GetAllItems<T>(this Enum value)
{
foreach (object item in Enum.GetValues(typeof(T)))
{
yield return (T)item;
}
}
}
class Program
{
[Flags]
public enum StoreType
{
Paypal = 1,
Plimus = 2,
Other = 3
};
static void Main(string[] args)
{
StoreType.GetAllItems //Fail here
}
}
}