Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to parse a flag enum to string

I have a class "license" which is a collection of a bunch of enum flags like this:

Public class License
{
    UsageType Usage { get; set; }
    PlatformType Platform { get; set; }

    public enum UsageType { read = 1, write = 2, wipe = 4, all = 7 }
    public enum PlatformType { windows = 1, linux = 2, ios = 4, all = 7 }

    etc...
}

The point is that the various flags of the same category can be OR'd together to form a profile of what the user can do with said license. Now I'm trying to display the values of "Usage" and "Platform" in a human-friendly way so for instance if Usage == UsageType.read | UsageType.write then it should be parsed to "read, write".

I did this successfully with a single enum type by testing the value for each flag and appending enumitem.ToString() for each flag it has to a string. Since I have a lot of these enums and values though, I'd like to come up with a more generic approach.

I came up with this (below) but since I'm not very familiar with template functions in c# so I don't know why this doesn't work but at least it should illustrate what i mean:

private string parseEnum<T>(T value)
{
    string ret = "";
    foreach (var ei in (T[])Enum.GetValues(typeof(T)))
    {
        if (value.HasFlag(ei)) ret += ei.ToString() + ", ";
    }
    ret = ret.substring(0, ret.Length-1);
    return ret;
}

It's saying that T does not contain a definition for "HasFlag" but how could it now that if it doesn't know what T is?

like image 906
user81993 Avatar asked Oct 01 '14 20:10

user81993


People also ask

Can enums be strings?

In a string enum, each member has to be constant-initialized with a string literal, or with another string enum member. While string enums don't have auto-incrementing behavior, string enums have the benefit that they “serialize” well.

What is enum parse?

The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object. The following is our enumeration. enum Vehicle { Car, Bus, Truck, Motobike }; Now, use the GetNames() method in a loop to get the enum values.

What is a flag enum?

Enum Flags Attribute The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators.


1 Answers

You should use the FlagsAttribute, which causes the built-in ToString and Enum.Parse methods to work just the way you want. Also note that the convention is that flags enum names should be plural, so e.g. UsageTypes instead of UsageType.

[Flags]
public enum UsageTypes { Read = 1, Write = 2, Wipe = 4, All = 7 }
[Flags]
public enum PlatformTypes { Windows = 1, Linux = 2, iOs = 4, All = 7 }

var e1 = License.UsageTypes.Read | License.UsageTypes.Write;
var s = e1.ToString();
Debug.Assert(s == "Read, Write");
var e2 = (License.UsageTypes)Enum.Parse(typeof(License.UsageTypes), s);
Debug.Assert(e1 == e2);
like image 172
Tim S. Avatar answered Sep 21 '22 23:09

Tim S.