Attributes are wonderful for decorating your Enums with additional markup and meta data. However, looking up attributes via reflection is not always a fast operation in terms of performance. Additionally, no one likes typing that code over and over again.
Well not to worry, just use the following extension methods to help cache your Enum attribute look ups, and increase your application's performance! But how much faster is this? Good question...
Iterations | Average Elapsed Ticks | Difference | |
---|---|---|---|
No Cache | With Cache | ||
2 | 1182.00 | 3934.00 | 4x Slower |
10 | 20.10 | 7.10 | 3x Faster |
100 | 13.07 | 1.37 | 10x Faster |
1,000 | 13.27 | 1.40 | 10x Faster |
10,000 | 13.56 | 1.45 | 10x Faster |
100,000 | 13.02 | 1.33 | 10x Faster |
Enum Extensions Code
public static class EnumExtensions
{
private static readonly
ConcurrentDictionary<Tuple<bool, Type, Type, Enum>, Attribute>
AttributeMap =
new ConcurrentDictionary<Tuple<bool, Type, Type, Enum>, Attribute>();
/// <summary>
/// Gets the specified attribute from an enum via a cache.
/// </summary>
public static T GetAttribute<T>(this Enum value, bool inherit = true)
where T : Attribute
{
var attrType = typeof(T);
var valueType = value.GetType();
var key = new Tuple<bool, Type, Type, Enum>(
inherit, attrType, valueType, value);
var attribute = AttributeMap.GetOrAdd(key, GetAttribute);
return attribute as T;
}
private static Attribute GetAttribute(Tuple<bool, Type, Type, Enum> key)
{
var valueString = key.Item4.ToString();
var field = key.Item3.GetField(valueString);
return Attribute.GetCustomAttribute(field, key.Item2, key.Item1);
}
/// <summary>
/// Returns a description of the enum if provided via a Description
/// attribute. If not provided, the normal name is returned
/// </summary>
public static string Description(this Enum value)
{
return value.Description<DescriptionAttribute>();
}
/// <summary>
/// Returns a description of the enum if provided via an extended
/// Description attribute. If not provided, the normal name is returned
/// </summary>
public static string Description<T>(this Enum value)
where T : DescriptionAttribute
{
var attribute = value.GetAttribute<T>();
return attribute != null
? attribute.Description
: value.ToString();
}
}
Unit Tests
public class EnumExtensionsTests
{
public class TestAttribute : Attribute
{
public string Message { get; set; }
public TestAttribute(string message) { Message = message; }
}
public enum TestEnum
{
[Description("Hello world!")]
HelloWorld = 1,
[Test("Goodnight, moon?")]
GoodnightMoon = 2
}
[Fact]
public void DescriptionTest()
{
var helloWorld = TestEnum.HelloWorld.Description();
Assert.Equal("Hello world!", helloWorld);
var goodnightMoon = TestEnum.GoodnightMoon.Description();
Assert.Equal("GoodnightMoon", goodnightMoon);
}
[Fact]
public void GetAttributeTest()
{
var helloWorld = TestEnum.HelloWorld.GetAttribute<TestAttribute>();
Assert.Null(helloWorld);
var goodnightMoon = TestEnum.GoodnightMoon.GetAttribute<TestAttribute>();
Assert.Equal("Goodnight, moon?", goodnightMoon.Message);
}
}
Enjoy,
Tom
It’s important that you submit a quality essay because a poor one can jeopardize your academic career. This is why many students turn to a essay writing service to help them finish their essays.essays
ReplyDeleteThe information was usefull but not cleared
ReplyDelete"Insane Hot Sauce melted my face off… and I’d gladly do it again. Best burn ever!"
ReplyDelete"This isn’t just hot sauce. It’s a rite of passage for spice lovers!"
Insainhotsauce
”Montano Roofing LLC offers expert guidance on natural roofing materials like wood shakes, slate, and clay tiles, which provide durability and a classic look. If you're in Norwalk and searching for a trusted Norwalk roofer, choosing the right material is key to longevity and weather resistance. Have you considered natural roofing for your home? Let’s discuss the best options for your needs!"
ReplyDelete🔥 north carolina hot saucebrings the perfect blend of heat and Southern flavor! 🌶️🔥 Whether you're a BBQ lover or a spice fanatic, this sauce is a must-try. Bold, zesty, and packed with Carolina pride!"🔥 north carolina hot saucebrings the perfect blend of heat and Southern flavor! 🌶️🔥 Whether you're a BBQ lover or a spice fanatic, this sauce is a must-try. Bold, zesty, and packed with Carolina pride!"
ReplyDeleteMuch informative and valuable!! Discover polo shirt brands in pakistan
ReplyDeleteMuch informative and valuable!! Discover saltwater fish food
ReplyDelete"If you love spicy food, HotSauceStore is the place to be! Amazing selection, great quality, and fast delivery. Definitely coming back for more!" 🔥🌶️
ReplyDeletehot sauce store
"Want to buy a Samsung laptop in Pakistan at the best price? Explore the latest models and updated prices on M3 Computer for top deals and discounts!"
ReplyDeletesamsung laptop price in pakistan
"The HP ProBook 650 G2 is a great business laptop with a sturdy design, solid performance, and excellent security features. Whether you're upgrading or need reliable repairs, check out M3 Computer for the best solutions!"
ReplyDeletehp probook 650 g2
ReplyDelete"I love trying out different hot sauces, and this article has some great tips! If you're into bold flavors with a perfect balance of heat and sweetness, check out Berry Bomb Hot Sauce. It’s packed with fruity spice that makes everything taste amazing!"
berry bomb hot sauce
Much informative and valuable!! Discover
ReplyDeletefish store salem oregon
Much informative and valuable!! Discover best t shirts, underwear and much more at
ReplyDeleteMendeez Pakistan
"Upgrade your work setup with the Dell Latitude E7450! Get the best price at M3 Computer."
ReplyDeletedell latitude e7450 price in pakistan
"Upgrade your tech with a Samsung laptop! Visit M3 Computer for the best prices in Pakistan."
ReplyDeletesamsung laptop price in pakistan
Much informative and valuable!! Discover men boxers pakistan
ReplyDeleteMama said good food brings the family together—and she was right! Your recipes are the kind of love-filled, comforting dishes that make every meal feel like home. Can’t wait to try them all
ReplyDelete