I am trying to implement an enumeration class found at https://github.com/jbogard/presentations/blob/master/WickedDomainModels/After/Model/Enumeration.cs.
In the following code, I am getting a compile error that GetFields
cannot be resolved.
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>();
}
According to http://msdn.microsoft.com/en-us/library/ch9714z3(v=vs.110).aspx, this method is supported in Portable Class Libraries.
My library is targeting .NET for Windows Store apps, .NET Framework 4.5 and Windows Phone 8.
Any idea of what is going on here?
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));
}
To add to Damien's answer, in .Net for Windows Store Apps, you can use the following extension method:
using System.Reflection;
var fields = type.GetRuntimeFields();
http://msdn.microsoft.com/en-us/library/system.reflection.runtimereflectionextensions.getruntimefields.aspx
This seems to be the equivalent of the GetFields
method for the .Net Framework.
This method returns all fields that are defined on the specified type, including inherited, non-public, instance, and static fields.
Just because a method says it's supported in Portable Class Libraries doesn't mean that it's supported for all possible targets. If you look at the help for the Type
class, it lists each member and shows icons for each supported system.
In this case, you'll notice that there's no green shopping bag icon next to GetFields
- it's not supported in Windows Store apps, and so as long as you include Windows Store in your set of supported targets for the PCL, it will not be available.
Another way to put it is - in the Version Information block for the methods, if they're supported for Windows Store, there'll be a specific section saying about it. Compare GetGenericTypeDefinition
:
.NET Framework
Supported in: 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client Profile
Supported in: 4, 3.5 SP1
Portable Class Library
Supported in: Portable Class Library
.NET for Windows Store apps
Supported in: Windows 8
to GetFields
.NET Framework
Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
.NET Framework Client Profile
Supported in: 4, 3.5 SP1
Portable Class Library
Supported in: Portable Class Library
For Windows Store apps, for reflection, you should switch to using the TypeInfo
class - but note that it still doesn't, specifically, support the GetFields
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With