3
對於枚舉,Silverlight缺少GetValues,所以我想我會編寫一個擴展方法來覆蓋我的項目中的需求。唯一的問題是,我不確定擴展方法的簽名應該是什麼樣子。林思考沿線的東西:在Silverlight中爲枚舉定義擴展方法
public static IEnumerable<Enum> GetValues(this Enum e)
但它沒有顯示在intellisense,所以我知道我錯了。任何指針?
對於枚舉,Silverlight缺少GetValues,所以我想我會編寫一個擴展方法來覆蓋我的項目中的需求。唯一的問題是,我不確定擴展方法的簽名應該是什麼樣子。林思考沿線的東西:在Silverlight中爲枚舉定義擴展方法
public static IEnumerable<Enum> GetValues(this Enum e)
但它沒有顯示在intellisense,所以我知道我錯了。任何指針?
我想我想通了由一個小reflection結合起來,並在反射挖掘:
public static Array GetValues(this Enum enumType)
{
Type type = enumType.GetType();
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
Array array = Array.CreateInstance(type, fields.Length);
for (int i = 0; i < fields.Length; i++)
{
var obj = fields[i].GetValue(null);
array.SetValue(obj, i);
}
return array;
}