Silverlight 缺少 GetValues对于枚举,所以我想我会写一个扩展方法来满足我在项目中的需求。唯一的问题是,我不确定扩展方法的签名应该是什么样子。我在想一些事情:
public static IEnumerable<Enum> GetValues(this Enum e)
但它没有出现在智能感知中,所以我知道我错了。有什么指点吗?
最佳答案
我想我是通过结合一点点 reflection 来解决这个问题的使用并挖掘 Reflector:
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;
}
关于c# - 在 Silverlight 中为枚举定义扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2238334/