Created
December 31, 2009 17:48
-
-
Save actaneon/266816 to your computer and use it in GitHub Desktop.
Enum Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Purpose is to have strongly typed options for in code | |
| // and be able to use those options in list operations | |
| // as well as have more than two fields. | |
| // Usage: | |
| // Fruit item = Fruit.GetItems().First(x => x.ID = 1); | |
| // Assert.IsTrue(item.Code = "a"); | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| public class BaseEnumClass<T> | |
| { | |
| public static IEnumerable<T> GetItems() | |
| { | |
| object obj = null; | |
| return typeof(T).GetFields().Select(item => (T)item.GetValue(obj)); | |
| } | |
| } | |
| public class Fruit : BaseEnumClass<Fruit> | |
| { | |
| public static readonly Fruit Apple = new Fruit(1, "a", "Apple"); | |
| public static readonly Fruit Bananna = new Fruit(2, "b", "Bananna"); | |
| public static readonly Fruit Cantelope = new Fruit(3, "c", "Cantelope"); | |
| public Fruit(int id, string code, string name) | |
| { | |
| this.ID = id; | |
| this.Code = code; | |
| this.Name = name; | |
| } | |
| public int ID { get; set; } | |
| public string Code { get; set; } | |
| public string Name { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment