views:

110

answers:

2

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?

+2  A: 

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.

Jon Skeet
You win again, Jon. :) @GenericTypeTea, see http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx for a complete explanation.
Stephen Cleary
Doesn't seem to work! Just gonna read that article and see what I'm missing. If I add `static Enumeration() {}` then that fires, but `static DocketType() {}` doesn't.
GenericTypeTea
@GenericTypeTea: Editing...
Jon Skeet
Downvoted because **this answer is wrong** (it doesn’t address the question).
Timwi
People are very keen to downvote you, Mr. Skeet. I knew this was a code smell when I wrote it: http://stackoverflow.com/questions/3377597/whats-the-correct-way-of-retrieving-my-custom-enumeration-classes-by-their-value. +1 for the assist.
GenericTypeTea
@GenericTypeTea: It's reasonable to have downvoted the pre-edit version, given that I'd basically misunderstood the problem. I hope people will undo the downvotes, but I'm not sure the "yeah, that sucks" is worth an upvote ;) But yes, it's a code smell and I think you'll find it hard to work round with the current design. I'll think about it...
Jon Skeet
I am not keen to downvote Jon, I’m keen to downvote wrong answers. And frankly, I’m annoyed when people upvote (and/or defend) answers just because they’re Jon’s (or Eric Lippert’s etc.), with no regard as to whether the answer is correct.
Timwi
@Jon - I think for the time being I'll just add a `Resolve` method to the `DocketType` class and have a `ThisSucksResolve` protected method in the base class which it calls.
GenericTypeTea
@Timwi: Yeah, I had no problem with the pre-edit downvote - I'd definitely misread the issue, without a doubt.
Jon Skeet
@Jon Skeet: I’m sure you had no problem with it because I know you are a very reasonable person — but look at how many *other* people have a problem with it *just because you’re famous*.
Timwi
@Timwi: Well, in this case it's only one - but yes, your point is taken. Votes should never be cast in either direction just because of the poster.
Jon Skeet
@Timwi - I didn't have a problem with it as such, I just believe it's in the community spirit to first explain to your peer why their answer is wrong and if they don't do anything about it, do a downvote. I.e. I didn't upvote Jon for his first answer, I upvoted him once he'd edited. I'm not sure a 2-3 minute turnaround from an honest mistake to a corrected answer is worth a downvote. I wouldn't enjoy SO if I got downvoted for every mistake I made (and I make a lot). Anyway, us little people need to just face the fact that some people do suffer the Jon Skeet effect and don't see past the rep.
GenericTypeTea
Plus, it's always awesome to beat a high rep user to an answer, consider it a challenge!
GenericTypeTea
+6  A: 

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.
Timwi
Just to reply to your first paragraph, it would be reasonable for the CLR to initialize types used as type arguments. It would be reasonable for that to be guaranteed, even. It just doesn't happen :(
Jon Skeet
Yeah, I never liked the design anyway: http://stackoverflow.com/questions/3377597/whats-the-correct-way-of-retrieving-my-custom-enumeration-classes-by-their-value. I just did it because I couldn't come up with an alternative.
GenericTypeTea
Oops, forgot the +1. Thanks for confirming my fears. Back to the drawing board!
GenericTypeTea