Credo che questo sia lo stesso di .NET Compact Framework. Se si presuppone che i valori dell'enumerazione inizino a 0 e utilizzino ogni valore fino a quando il loro intervallo non si esaurisce, il seguente codice dovrebbe funzionare.
public static IList<int> GetEnumValues(Type oEnumType)
{
int iLoop = 0;
bool bDefined = true;
List<int> oList = new List<int>();
//Loop values
do
{
//Check if the value is defined
if (Enum.IsDefined(oEnumType, iLoop))
{
//Add item to the value list and increment
oList.Add(iLoop);
++iLoop;
}
else
{
//Set undefined
bDefined = false;
}
} while (bDefined);
//Return the list
return oList;
}
Ovviamente si potrebbe modificare il codice per restituire i nomi enum o per abbinare i modelli diferent esempio valori bit a bit.
Ecco una versione alternativa del metodo che restituisce un IList<EnumType>
.
public static IList<T> GetEnumValues<T>()
{
Type oEnumType;
int iLoop = 0;
bool bDefined = true;
List<T> oList = new List<T>();
//Get the enum type
oEnumType = typeof(T);
//Check that we have an enum
if (oEnumType.IsEnum)
{
//Loop values
do
{
//Check if the value is defined
if (Enum.IsDefined(oEnumType, iLoop))
{
//Add item to the value list and increment
oList.Add((T) (object) iLoop);
++iLoop;
}
else
{
//Set undefined
bDefined = false;
}
} while (bDefined);
}
//Return the list
return oList;
}
È ridicolo che questo non sia nel quadro. La dimensione del download non è una scusa per torturare gli sviluppatori. –
se qualcuno ha bisogno, in particolare, di GetEnumValues () e GetEnumNames(); Li ho implementati (in base alla risposta di ptoinson e Shimmy in basso) come risposta alla seguente domanda: http://stackoverflow.com/questions/7062208/how-do-i-create-a-getenumvalues-extension-method -in-silverlight-that-works-the-sa, e penso che siano equivalenti alle normali funzioni .NET (ma non Silverlight) con lo stesso nome. – shelleybutterfly