<p>This is probably best shown with an example. I have an enum with attributes:</p>

<pre><code>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,
}
</code></pre>

<p>I want to get to those attributes from an instance:</p>

<pre><code>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
}
</code></pre>

<p>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.</p>

<p>Does anyone have a better way?</p>

[{'answer_id': 17807, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 5, 'selected': True, 'text': "<p>This is probably the easiest way.</p>\n\n<p>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. </p>\n\n<p>For example code to emit a getter from a property</p>\n\n<pre><code>public delegate object FastPropertyGetHandler(object target);    \n\nprivate static void EmitBoxIfNeeded(ILGenerator ilGenerator, System.Type type)\n{\n    if (type.IsValueType)\n    {\n        ilGenerator.Emit(OpCodes.Box, type);\n    }\n}\n\npublic static FastPropertyGetHandler GetPropertyGetter(PropertyInfo propInfo)\n{\n    // generates a dynamic method to generate a FastPropertyGetHandler delegate\n    DynamicMethod dynamicMethod =\n        new DynamicMethod(\n            string.Empty, \n            typeof (object), \n            new Type[] { typeof (object) },\n            propInfo.DeclaringType.Module);\n\n    ILGenerator ilGenerator = dynamicMethod.GetILGenerator();\n    // loads the object into the stack\n    ilGenerator.Emit(OpCodes.Ldarg_0);\n    // calls the getter\n    ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetGetMethod(), null);\n    // creates code for handling the return value\n    EmitBoxIfNeeded(ilGenerator, propInfo.PropertyType);\n    // returns the value to the caller\n    ilGenerator.Emit(OpCodes.Ret);\n    // converts the DynamicMethod to a FastPropertyGetHandler delegate\n    // to get the property\n    FastPropertyGetHandler getter =\n        (FastPropertyGetHandler) \n        dynamicMethod.CreateDelegate(typeof(FastPropertyGetHandler));\n\n\n    return getter;\n}\n</code></pre>\n"}, {'answer_id': 17848, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 3, 'selected': False, 'text': "<p>I generally find reflection to be quite speedy as long as you don't dynamically invoke methods.<br>\nSince you are just reading the Attributes of an enum, your approach should work just fine without any real performance hit.</p>\n\n<p>And remember that you generally should try to keep things simple to understand. Over engineering this just to gain a few ms might not be worth it.</p>\n"}]
['https://Stackoverflow.com/questions/17772', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/905/']