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 7708471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:37:13+00:00 2026-06-01T00:37:13+00:00

Anybody know what the best to use to read a XmlEnumAttribute Option 1: With

  • 0

Anybody know what the best to use to read a XmlEnumAttribute

Option 1: With GetMember

    public static string XmlEnum(this Enum e)
    {
        Type type = e.GetType();
        MemberInfo[] memInfo = type.GetMember(e.ToString());
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(XmlEnumAttribute), false);
            if (attrs != null && attrs.Length > 0)
            {
                return ((XmlEnumAttribute)attrs[0]).Name;
            }
        }
        return e.ToString();
    }

Option 2: With GetField

    public static string XmlEnum2(this Enum e)
    {
        Type type = e.GetType();
        FieldInfo info = type.GetField(e.ToString());
        if (!info.IsDefined(typeof(XmlEnumAttribute), false))
        {
            return e.ToString();
        }
        object[] attrs = info.GetCustomAttributes(typeof(XmlEnumAttribute), false);
        return ((XmlEnumAttribute)attrs[0]).Name;
    }
  • 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-06-01T00:37:14+00:00Added an answer on June 1, 2026 at 12:37 am

    Why not try 100,000 times and see how long it takes in each case?

    Because that doesn’t test a realistic usage scenario for an attribute. It is expensive the first time you dig up an attribute, cheap after that. The expense is loading the IL for the attribute class and compiling it, locating the attribute data in the assembly metadata and loading it from disk. Then calling the attribute constructor and assigning the attribute properties. The cost of your code to read the attribute is peanuts compared to that, it’s the disk I/O that’s expensive by several orders of magnitude. The second time you retrieve the attribute, a lot of that work is done and it will be quick, just the object initialization from data that’s cached.

    You normally read an attribute only once, maybe a few times. So the cost is dominated by the expensive first time, the code you use matters very little. Go ahead and profile it. Just make sure you don’t treat that expensive first time as “experimental error”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using .NET's Office interop libraries, does anybody know the best way to convert back
Does anybody know what is the best approach to accessing a sql view through
Anybody know how to get gmail's OpenID working? All I find is this http://openid-provider.appspot.com/
Does anybody know best aproach for work with web forms(open/fill/submit) ? For example there
Anybody know what the proper mime-type for patch files would be? I have been
Anybody know of a way to copy a file from path A to path
Anybody know what my DLLImport statement should look like here: extern C __declspec(dllexport) long
Anybody know of a way to batch NHibernate queries using NHibernate.Linq like you can
Anybody know how to get all of these in a C# app that leads
Anybody know of a good 3rd Party grid control that supports AutoFill (like Excel

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.