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

  • Home
  • SEARCH
  • 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 39265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:50:16+00:00 2026-05-10T14:50:16+00:00

This is probably best shown with an example. I have an enum with attributes:

  • 0

This is probably best shown with an example. I have an enum with attributes:

public enum MyEnum {      [CustomInfo('This is a custom attrib')]     None = 0,      [CustomInfo('This is another attrib')]     ValueA,      [CustomInfo('This has an extra flag', AllowSomething = true)]     ValueB, } 

I want to get to those attributes from an instance:

public CustomInfoAttribute GetInfo( MyEnum enumInput ) {      Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum )      //here is the problem, GetField takes a string     // the .ToString() on enums is very slow     FieldInfo fi = typeOfEnum.GetField( enumInput.ToString() );      //get the attribute from the field     return fi.GetCustomAttributes( typeof( CustomInfoAttribute  ), false ).         FirstOrDefault()        //Linq method to get first or null         as CustomInfoAttribute; //use as operator to convert } 

As this is using reflection I expect some slowness, but it seems messy to convert the enum value to a string (which reflects the name) when I already have an instance of it.

Does anyone have a better way?

  • 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. 2026-05-10T14:50:16+00:00Added an answer on May 10, 2026 at 2:50 pm

    This is probably the easiest way.

    A quicker way would be to Statically Emit the IL code using Dynamic Method and ILGenerator. Although I’ve only used this to GetPropertyInfo, but can’t see why you couldn’t emit CustomAttributeInfo as well.

    For example code to emit a getter from a property

    public delegate object FastPropertyGetHandler(object target);      private static void EmitBoxIfNeeded(ILGenerator ilGenerator, System.Type type) {     if (type.IsValueType)     {         ilGenerator.Emit(OpCodes.Box, type);     } }  public static FastPropertyGetHandler GetPropertyGetter(PropertyInfo propInfo) {     // generates a dynamic method to generate a FastPropertyGetHandler delegate     DynamicMethod dynamicMethod =         new DynamicMethod(             string.Empty,              typeof (object),              new Type[] { typeof (object) },             propInfo.DeclaringType.Module);      ILGenerator ilGenerator = dynamicMethod.GetILGenerator();     // loads the object into the stack     ilGenerator.Emit(OpCodes.Ldarg_0);     // calls the getter     ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetGetMethod(), null);     // creates code for handling the return value     EmitBoxIfNeeded(ilGenerator, propInfo.PropertyType);     // returns the value to the caller     ilGenerator.Emit(OpCodes.Ret);     // converts the DynamicMethod to a FastPropertyGetHandler delegate     // to get the property     FastPropertyGetHandler getter =         (FastPropertyGetHandler)          dynamicMethod.CreateDelegate(typeof(FastPropertyGetHandler));       return getter; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 84k
  • Answers 84k
  • 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 Benchmarking a runtime environment as complex as the JVM is… May 11, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer You can refactor your TournamentPairing class to be more "round"… May 11, 2026 at 5:00 pm
  • Editorial Team
    Editorial Team added an answer Some variation on VERP is probably the most reliable... http://en.wikipedia.org/wiki/Variable_envelope_return_path… May 11, 2026 at 5:00 pm

Related Questions

Is it a good idea to quote keys when using a hash in Perl?
I wrote a device controller (rs232) and it is being used successfully, however users
I want to auto-generate a HTML table from some custom data. One of the
A follow-up from this question , I've changed my controller and routing around so

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.