c# – GetFields是否支持PCL?
发布时间:2020-12-15 06:26:42 所属栏目:百科 来源:网络整理
导读:我试图实现一个在 https://github.com/jbogard/presentations/blob/master/WickedDomainModels/After/Model/Enumeration.cs发现的枚举类. 在下面的代码中,我收到一个编译错误,GetFields无法解析. public static IEnumerableT GetAllT() where T : Enumeratio
我试图实现一个在
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)); } 解决方法
要添加到Damien的答案,在.Net中的Windows Store Apps,您可以使用以下扩展方法:
using System.Reflection; var fields = type.GetRuntimeFields(); http://msdn.microsoft.com/en-us/library/system.reflection.runtimereflectionextensions.getruntimefields.aspx 这似乎与.NET Framework的GetFields方法相当.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |