我正在尝试将扩展方法添加到 Enum 类型,但下面的代码失败。编译器在线给出错误StoreType.GetAllItems
如何将扩展方法添加到 Enum 类型?
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
}
}
}