I have been causing some headaches in the last couple of weeks with a curiously repeating pattern .
Following these two of my questions:
How can I improve the following example:
public class DocketType : Enumeration<DocketType, int, string>
{
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) { }
}
I need a static method that I do not need to repeat in the class Enumeration:
public abstract class Enumeration<TEnum, X, Y> : IComparable
where TEnum : Enumeration<TEnum, X, Y>
{
protected Enumeration(X value, Y displayName)
{
AddToStaticCache(this);
}
public static TEnum Resolve(X value)
{
return Cache[value] as TEnum;
}
}
The problem with this, as you will see from my second related question, is that the call Enumeration<DocketType, int, string>.Resolve(X value);does not cause the creation of static objects DocketType.
. , . ChildResolve, Resolve Enumeration. !
:
, , :
static Enumeration()
{
GetAll();
}
public static void GetAll()
{
var type = typeof(TEnum);
var fields = type.GetFields(BindingFlags.Public |
BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in fields)
{
var locatedValue = info.GetValue(null) as Enumeration<TEnum, X, Y>;
Cache.Add(locatedValue.Value, locatedValue);
}
}
, CodeCampServer MVC, !