Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:02:29+00:00 2026-05-15T12:02:29+00:00

I need a Generic function to retrieve the name or value of an enum

  • 0

I need a Generic function to retrieve the name or value of an enum based on the XmlEnumAttribute “Name” property of the enum. For example I have the following enum defined:

Public Enum Currency
   <XmlEnum("00")> CDN = 1
   <XmlEnum("01")> USA= 2
   <XmlEnum("02")> EUR= 3
   <XmlEnum("03")> JPN= 4
End Enum

The first Currency enum value is 1; the enum name is “CDN”; and the XMLEnumAttribute Name property value is “00”.

If I have the enum value, I can retrieve the XmlEnumAttribute “Name” value using the following generic function:

Public Function GetXmlAttrNameFromEnumValue(Of T)(ByVal pEnumVal As T) As String

        Dim type As Type = pEnumVal.GetType
        Dim info As FieldInfo = type.GetField([Enum].GetName(GetType(T), pEnumVal))
        Dim att As XmlEnumAttribute = CType(info.GetCustomAttributes(GetType(XmlEnumAttribute), False)(0), XmlEnumAttribute) 'If there is an xmlattribute defined, return the name

        Return att.Name
    End Function

So using the above function, I can specify the Currency enum type, pass a value of 1, and the return value will be “00”.

What I need is a function to perform if the opposite. If I have the XmlEnumAttribute Name value “00”, I need a function to return a Currency enum with a value of 1. Just as useful would be a function that would return the enum name “CDN”. I could then simply parse this to get the enum value.

Any assistance would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:02:29+00:00Added an answer on May 15, 2026 at 12:02 pm

    A requirement to solve this exact same problem led me to this question and answer. As I develop in VB.NET, I rewrote CkH’s solution into VB and modified it to use your GetXmlAttrNameFromEnumValue function.

    Public Shared Function GetCode(Of T)(ByVal value As String) As T
        For Each o As Object In System.Enum.GetValues(GetType(T))
            Dim enumValue As T = CType(o, T)
            If GetXmlAttrNameFromEnumValue(Of T)(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase) Then
                Return CType(o, T)
            End If
        Next
    
        Throw New ArgumentException("No code exists for type " + GetType(T).ToString() + " corresponding to value of " + value)
    End Function
    

    C# Version:

    public static string GetXmlAttrNameFromEnumValue<T>(T pEnumVal)
    {
        // http://stackoverflow.com/q/3047125/194717
        Type type = pEnumVal.GetType();
        FieldInfo info = type.GetField(Enum.GetName(typeof(T), pEnumVal));
        XmlEnumAttribute att = (XmlEnumAttribute)info.GetCustomAttributes(typeof(XmlEnumAttribute), false)[0];
        //If there is an xmlattribute defined, return the name
    
        return att.Name;
    }
    public static T GetCode<T>(string value)
    {
        // http://stackoverflow.com/a/3073272/194717
        foreach (object o in System.Enum.GetValues(typeof(T)))
        {
            T enumValue = (T)o;
            if (GetXmlAttrNameFromEnumValue<T>(enumValue).Equals(value, StringComparison.OrdinalIgnoreCase))
            {
                return (T)o;
            }
        }
    
        throw new ArgumentException("No XmlEnumAttribute code exists for type " + typeof(T).ToString() + " corresponding to value of " + value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer call(map); implicitly converts map to a function pointer, to make… May 16, 2026 at 4:29 am
  • Editorial Team
    Editorial Team added an answer It would appear that my other projects had the merge… May 16, 2026 at 4:29 am
  • Editorial Team
    Editorial Team added an answer Is there any particular reason you need to do this?… May 16, 2026 at 4:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.