Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is GetFields supported in a PCL?

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?

Solution

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));
}     
like image 926
mattruma Avatar asked Oct 22 '13 08:10

mattruma


2 Answers

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.

like image 56
dcastro Avatar answered Nov 17 '22 12:11

dcastro


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:

enter image description here

.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

enter image description here

.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.

like image 25
Damien_The_Unbeliever Avatar answered Nov 17 '22 13:11

Damien_The_Unbeliever