Return Enum C#

Asked

Viewed 1,002 times

1

I have the following:

 public enum TNFeInfNFeDetImpostoICMSICMS00CST
 {

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("00")]
    item00 = 00,
 }

How do I "take" the "00" value of the Enum in question ?

I know you usually do it this way:

var x = (int)enumQualquer

However it returns "0", only a zero, which is to be expected because of the int.

But I need the "00" value of Enum.

How do I do?

2 answers

5


you will have to take the attribute XmlEnumAttribute of your enumerator, then read the property Name.

using System;
using System.Linq;
using System.Xml.Serialization;

public enum TNFeInfNFeDetImpostoICMSICMS00CST
{
    [XmlEnumAttribute("00")]
    item00 = 00
}

public static class EnumExtensions
{
    public static T GetCustomAttribute<T>(this Enum enumerador)
    {
        var membro = enumerador.GetType().GetMember(enumerador.ToString()).FirstOrDefault();
        if (membro != null)
        {
            return (T)membro.GetCustomAttributes(typeof(T), false).FirstOrDefault();
        }
        return default(T);
    }
}

public class Program
{
    public static void Main()
    {

        var atributo = TNFeInfNFeDetImpostoICMSICMS00CST.item00.GetCustomAttribute<XmlEnumAttribute>().Name;
        Console.WriteLine(atributo);
    }
}

Dotnetfiddle

  • Thank you. Solved.

1

You have to take the attribute that has the text you want. You can use this:

public static class EnumExt {
    public static string GetAttributeDescription(this Enum enumValue) {
        var attributes = enumValue.GetType().GetMember(enumValue.ToString())[0].GetCustomAttributes(typeof(XmlEnumAttribute), false);
        return (attributes.Length > 0) ? ((XmlEnumAttribute)attributes[0]).Name : String.Empty;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I would rethink some things. I don’t know if this is a case to use an enumeration. Enumerations cannot be subject to changes in legislation or actions that you have no control over. This will cause huge maintenance problems. I have experience with this and I can say that you will regret using it in place of a simple table (a dictionary or a specialized type. These data are inherently mutable.

The name already indicates that something is wrong. It has become so confused that it has little use. Even if it were an enumeration, the form used seems to be wrong. It seems to have enumerations within the enumeration name. The appeal seems to be quite wrong.

even if it were to adopt this solution would need to do in a way that this content is more meaningful and easy to organize and manipulate.

Maybe I already made the whole system thinking about it. Still I would reformulate everything. It is a serious conceptual error that will bring so many disorders that remaking is already the best solution.

  • 1

    There is recompilation :)

  • 1

    And that’s the least of the problems :)

  • @bigown, thank you for your comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.