Tutorial :Why are my static objects not being instantiated when first access to the static class is a static method on the base class?


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?


I don’t think the fields in DocketType should be initialised when all you’re accessing is Enumeration<>. You are not referencing the DocketType type at all when you call Enumeration<>.Resolve(). Should the CLR really initialise all subclasses every time you access a static method or static field? It would slow down your code, and in most cases unnecessarily so.

You could try writing Docket.Resolve(), which C# allows you to do, but I don’t know whether this will compile into something different than before; the compiler might just turn it into Enumeration<DocketType, int, string>.Resolve() and you’re back to sqaure one.

To be honest, I am inclined to suggest that your code structure is flawed, and the problem you’re running into is a symptom of that. You shouldn’t have to rely on Cache containing something. You shouldn’t have to rely on some static type initialisation to have occurred when you’re not using that type.

Therefore, your options are:

  • Put a pointless reference to DocketType somewhere in your Main() method to ensure the initialisation happens, and live with the idea that your code structure may be flawed.
  • Move the static fields to another type, perhaps Enumeration<> itself, which alleviates the flaw but doesn’t completely solve it.
  • Think about the fundamental structure of your code and redesign it so that you don’t have to rely on the cache being filled.


EDIT: I didn't realise that you were only ever referring to the base type. That definitely has problems - nothing is guaranteed to run the type initializer for DocketType in that case. I thought you were calling a method in DocketType which then used the cache.

In this case, it wouldn't have worked before, either. Using a type as a generic type argument doesn't force type initialization as far as I'm aware, and that's what you're after.

I think you'll have a hard time getting this to work. Basically you want to provoke type initialization, and I don't know a good way of doing that. You can call the type initializer with reflection, but you'd have to be very careful to only do that once.

I agree with Timwi: I think your best solution would be to restructure your design so you don't need this.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »