Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TheCycoONE/969542ebb51c22dd1167de83ab2cf70f to your computer and use it in GitHub Desktop.
Save TheCycoONE/969542ebb51c22dd1167de83ab2cf70f to your computer and use it in GitHub Desktop.
public class MyEnum
{
public static readonly MyEnum Val1;
public static readonly MyEnum Val2;
public static MyEnum FromId(int id)
{
return MyEnumsById[id];
}
public static MyEnum FromName(string name)
{
return MyEnumsByName[name];
}
public static MyEnum FromOtherProp(string prop)
{
return MyEnumsByOtherProp[prop];
}
private static Dictionary<int, MyEnum> MyEnumsById;
private static Dictionary<string, MyEnum> MyEnumsByName;
private static Dictionary<string, MyEnum> MyEnumsByOtherProp;
static MyEnum()
{
MyEnumsById = new Dictionary<int, MyEnum>();
MyEnumsByName = new Dictionary<string, MyEnum>();
MyEnumsByOtherProp = new Dictionary<string, MyEnum>();
Val1 = new MyEnum ( 21, "val1Name", "foo" );
Val2 = new MyEnum ( 42, "val2Name", "bar" );
}
private MyEnum(int id, string name, string prop)
{
Id = id;
Name = name;
OtherProp = prop;
MyEnumsById.Add(id, this);
MyEnumsByName.Add(name, this);
MyEnumsByOtherProp.Add(prop, this);
}
public int Id { get; private set; }
public string Name { get; private set; }
public string OtherProp { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment