我试图实现一个在
https://github.com/jbogard/presentations/blob/master/WickedDomainModels/After/Model/Enumeration.cs发现的枚举类.
在下面的代码中,我收到一个编译错误,GetFields无法解析.
- public static IEnumerable<T> GetAll<T>() where T : Enumeration
- {
- var type = typeof(T);
- var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
- return fields.Select(info => info.GetValue(null)).OfType<T>();
- }
根据http://msdn.microsoft.com/en-us/library/ch9714z3(v=vs.110).aspx,便携式类库支持该方法.
我的图书馆针对Windows商店应用程序.NET Framework 4.5和Windows Phone 8.
有什么想法在这里发生了什么?
解
- public static IEnumerable<T> GetAll<T>() where T : Enumeration
- {
- var type = typeof(T);
- var fields = type.GetRuntimeFields().Where(x => x.IsPublic || x.IsStatic);
- return fields.Select(info => info.GetValue(null)).OfType<T>();
- }
- public static IEnumerable GetAll(Type type)
- {
- var fields = type.GetRuntimeFields().Where(x => x.IsPublic || x.IsStatic);
- return fields.Select(info => info.GetValue(null));
- }