Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EnumMemberAttribute and doing automatic string conversions

Tags:

c#

enums

I have the following code

[DataContract]
public enum StatusType
{
    [EnumMember(Value = "A")]
    All,
    [EnumMember(Value = "I")]
    InProcess,
    [EnumMember(Value = "C")]
    Complete,
}

I'd like to do the following:

 var s = "C";
 StatusType status = SerializerHelper.ToEnum<StatusType>(s);   //status is now StatusType.Complete
 string newString = SerializerHelper.ToEnumString<StatusType>(status);   //newString is now "C"

I've done the second part using DataContractSerializer (see code below), but it seems like a lot of work.

Am I missing something obvious? Ideas? Thanks.

    public static string ToEnumString<T>(T type)
    {
        string s;
        using (var ms = new MemoryStream())
        {
            var ser = new DataContractSerializer(typeof(T));
            ser.WriteObject(ms, type);
            ms.Position = 0;
            var sr = new StreamReader(ms);
            s = sr.ReadToEnd();
        }
        using (var xml = new XmlTextReader(s, XmlNodeType.Element, null))
        {
            xml.MoveToContent();
            xml.Read();
            return xml.Value;
        }
    }
like image 341
bryanjonker Avatar asked May 02 '12 17:05

bryanjonker


5 Answers

Here is my proposition - it should give you the idea on how to do this (check also Getting attributes of Enum's value):

public static string ToEnumString<T>(T type)
{
    var enumType = typeof (T);
    var name = Enum.GetName(enumType, type);
    var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
    return enumMemberAttribute.Value;
}

public static T ToEnum<T>(string str)
{
    var enumType = typeof(T);
    foreach (var name in Enum.GetNames(enumType))
    {
        var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
        if (enumMemberAttribute.Value == str) return (T)Enum.Parse(enumType, name);
    }
    //throw exception or whatever handling you want or
    return default(T);
}
like image 189
empi Avatar answered Oct 09 '22 05:10

empi


If your project references Newtonsoft.Json (what doesn't these days?!), then there is a simple one line solution that doesn't need reflection:

public static string ToEnumString<T>(T value)
{
   return JsonConvert.SerializeObject(value).Replace("\"", "");
}

public static T ToEnum<T>(string value)
{
   return JsonConvert.DeserializeObject<T>($"\"{value}\"");
}

The ToEnumString method will only work if you have the StringEnumConverter registered in your JsonSerializerSettings (see JavaScriptSerializer - JSON serialization of enum as string), e.g.

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Converters = { new StringEnumConverter() }
};

Another advantage of this method is that if only some of your enum elements have the member attribute, things still work as expected, e.g.

public enum CarEnum
{
    Ford,
    Volkswagen,
    [EnumMember(Value = "Aston Martin")]
    AstonMartin
}
like image 40
Simon Ness Avatar answered Oct 09 '22 05:10

Simon Ness


Using extensions and C# 7.3 constraints

    public static class EnumMemberExtensions
    {
        public static string ToEnumString<T>(this T type)
            where T : Enum
        {
            var enumType = typeof(T);
            var name = Enum.GetName(enumType, type);
            var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
            return enumMemberAttribute.Value;
        }

        public static T ToEnum<T>(this string str)
            where T : Enum
        {
            var enumType = typeof(T);
            foreach (var name in Enum.GetNames(enumType))
            {
                var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
                if (enumMemberAttribute.Value == str) return (T)Enum.Parse(enumType, name);
            }
            //throw exception or whatever handling you want or
            return default;
        }
    }
like image 33
GetoX Avatar answered Oct 09 '22 06:10

GetoX


You can use reflection to get the value of the EnumMemberAttribute.

public static string ToEnumString<T>(T instance)
{
    if (!typeof(T).IsEnum)
        throw new ArgumentException("instance", "Must be enum type");
    string enumString = instance.ToString();
    var field = typeof(T).GetField(enumString);
    if (field != null) // instance can be a number that was cast to T, instead of a named value, or could be a combination of flags instead of a single value
    {
        var attr = (EnumMemberAttribute)field.GetCustomAttributes(typeof(EnumMemberAttribute), false).SingleOrDefault();
        if (attr != null) // if there's no EnumMember attr, use the default value
            enumString = attr.Value;
    }
    return enumString;
}

Depending on how your ToEnum works, you might want to use this sort of approach there as well. Also, the type can be inferred when calling ToEnumString, e.g. SerializerHelper.ToEnumString(status);

like image 23
Tim S. Avatar answered Oct 09 '22 06:10

Tim S.


This example shows how to convert enums using the DescriptionAttribute, the EnumMemberAttribute and the property name:

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
    public static class EnumExtensions
    {
        public static T ToEnumByAttributes<T>(this string value)
            where T:Enum
        {
            var enumType = typeof(T);
            foreach (var name in Enum.GetNames(enumType))
            {
                var field = enumType.GetField(name);
                if(field == null) continue;

                var enumMemberAttribute = GetEnumMemberAttribute(field);
                if (enumMemberAttribute != null && enumMemberAttribute.Value == value)
                {
                    return (T)Enum.Parse(enumType, name);
                }

                var descriptionAttribute = GetDescriptionAttribute(field);
                if (descriptionAttribute != null && descriptionAttribute.Description == value)
                {
                    return (T)Enum.Parse(enumType, name);
                }

                if (name == value)
                {
                    return (T)Enum.Parse(enumType, name);
                }
            }

            throw new ArgumentOutOfRangeException(nameof(value), value, $"The value could not be mapped to type {enumType.FullName}");
        }

        public static string ToStringByAttributes(this Enum value)
        {
            var field = value
                .GetType()
                .GetField(value.ToString());

            if (field == null) return string.Empty;

            var enumMemberAttribute = GetEnumMemberAttribute(field);
            if (enumMemberAttribute != null)
            {
                return enumMemberAttribute.Value ?? string.Empty;
            }

            var descriptionAttribute = GetDescriptionAttribute(field);
            if (descriptionAttribute != null)
            {
                return descriptionAttribute.Description;
            }

            return value.ToString();
        }

        private static DescriptionAttribute? GetDescriptionAttribute(FieldInfo field)
        {
            return field
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .OfType<DescriptionAttribute>()
                .SingleOrDefault();
        }

        private static EnumMemberAttribute? GetEnumMemberAttribute(FieldInfo field)
        {
            return field
                .GetCustomAttributes(typeof(EnumMemberAttribute), false)
                .OfType<EnumMemberAttribute>()
                .SingleOrDefault();
        }
    }

NUnit Tests:

    [TestFixture]
    public sealed class EnumExtensionsTests
    {
        public enum TestEnum
        {
            [EnumMember(Value = "A")]
            Alpha,

            [Description("O")]
            Omega
        }

        [Test]
        public void ShouldSerialize_FromEnumAttribute()
        {
            var result = TestEnum.Alpha.ToStringByAttributes();
            Assert.That(result, Is.EqualTo("A"));
        }

        [Test]
        public void ShouldSerialize_FromDescriptionAttribute()
        {
            var result = TestEnum.Omega.ToStringByAttributes();
            Assert.That(result, Is.EqualTo("O"));
        }

        [Test]
        public void ShouldDeserialize_FromEnumAttribute()
        {
            var result = "A".ToEnumByAttributes<TestEnum>();
            Assert.That(result, Is.EqualTo(TestEnum.Alpha));
        }

        [Test]
        public void ShouldDeserialize_FromDescriptionAttribute()
        {
            var result = "O".ToEnumByAttributes<TestEnum>();
            Assert.That(result, Is.EqualTo(TestEnum.Omega));
        }

        [Test]
        public void ShouldDeserialize_FromPropertyName()
        {
            var result = "Alpha".ToEnumByAttributes<TestEnum>();
            Assert.That(result, Is.EqualTo(TestEnum.Alpha));
        }
    }
like image 45
MovGP0 Avatar answered Oct 09 '22 05:10

MovGP0