Following utility function EnumToArray takes an enum type and returns an array populated with each enum item. You can tweak the code a little bit to make the function to be EnumToList easily.
The sample code also demonstrates how to use Enumerable.Except to get the delta of two arrays of same type T.
using System;
using System.Collections.Generic;
using System.Linq;
namespace IEnumerableTest
{
public static class Utils
{
public static T[] EnumToArray<T>()
{
Type enumType = typeof(T);
if (enumType.BaseType != typeof(Enum))
{
throw new ArgumentException("T must be a System.Enum");
}
return (Enum.GetValues(enumType) as IEnumerable<T>).ToArray();
}
}
class Program
{
static void Main(string[] args)
{
//Convert Enum type to an array
RoleType[] allRoles = Utils.EnumToArray();
//Use IEnumerable.Except to get part of the array.
RoleType[] localUserRoles = new RoleType[] { RoleType.LocalAdmin, RoleType.LocalUser, RoleType.Guest };
RoleType[] domainUserRoles = allRoles.Except(localUserRoles).ToArray();
}
enum RoleType
{
DomainAdmin,
LocalAdmin,
DomainUser,
LocalUser,
Guest
}
}
}