我有以下课程:
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)
{
}
}
使用以下基类:
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;
}
}
我遇到的问题是,当第一次使用静态类是通过基类中的方法时Changeover
,Withdrawal
并没有被创建。即如果我打电话,那么将是空的。Installation
Resolve
Resolve
Cache
但是,如果我执行类似DocketType foo = DocketType.Changeover;
in 的操作Application_Start
,则会创建所有静态字段,然后Cache
具有所有三个值。
创建这些静态字段的正确方法是什么,以便这种情况有效?