I have the following class:
public class DocketType : Enumeration<DocketType, int, string>
{
public static DocketType ChangeOver = new DocketType(1, "Changeover");
public static DocketType Withdrawal = new DocketType(2, "Withdrawal");
public static DocketType Installation = new DocketType(3, "Installation");
private DocketType(int docketTypeId, string description)
: base(docketTypeId, description)
{
}
}
With the following base class:
public abstract class Enumeration<TEnum, X, Y> : IComparable
where TEnum : Enumeration<TEnum, X, Y>
where X : IComparable
where Y : IComparable
{
protected Enumeration(X value, Y displayName)
{
AddToStaticCache(this);
}
public static TEnum Resolve(X value)
{
return Cache[value] as TEnum;
}
}
The problem I have is that Changeover
, Withdrawal
and Installation
are not being created when the first time that the static class is used is via the Resolve
method in the base class. I.e. if I call Resolve
, then Cache
will be empty.
However, if I do something like DocketType foo = DocketType.Changeover;
in Application_Start
, then all of the static fields get created and then Cache
has all three values.
What's the correct way to create these static fields so this scenario works?