Enumeration Extension
Here is the simple extension of common enumeration operation. I usually use this on our BlastAsia projects
The Extension
using System; namespace Common { public static class EnumerationExtension { public static int ToValue(this Enum enumeration) { return Convert.ToInt32(enumeration); } public static string ToName(this Enum enumeration) { return Enum.GetName(enumeration.GetType(), enumeration); } } }
The Enumeration
namespace Utilities { public enum CardStatus { Inactive = 1, Deactivated = 2, Redeemed = 3, Active = 4 } }
The Unit Test
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using ATI.Redemption.Utilities; namespace Common.Test { [TestClass] public class EnumerationExtension { [TestMethod] public void TestIfReturnsValue() { Assert.AreEqual(3, CardStatus.Redeemed.ToValue()); } [TestMethod] public void TestIfReturnsName() { Assert.AreEqual("Redeemed", CardStatus.Redeemed.ToName()); } } }
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.