2

I understood from this answer that C# static field initializers "are executed... prior to the first use of a static field of that class," but that still produces results I didn't expect, at least with generic types.

Coming from the Java world, I was missing my rich enums, and I thought with C#'s more serious generics that I ought to be able to replicate them with a minimum of boilerplate. Here (stripped of some details, like comparability) is what I came up with:

public class AbstractEnum<T> where T : AbstractEnum<T>
{
    static readonly IDictionary<String, T> nameRegistry = new Dictionary<String, T>();

    readonly String name;

    protected AbstractEnum (String name)
    {
        this.name = name;
        nameRegistry[name] = (T) this;
    }

    public String Name {
        get {
            return name;
        }
    }

    public static T ValueOf(String name) {
        return nameRegistry[name];
    }

    public static IEnumerable<T> Values {
        get {
            return nameRegistry.Values;
        }
    }
}

And some example subclasses:

public class SomeEnum : AbstractEnum<SomeEnum> {

    public static readonly SomeEnum V1 = new SomeEnum("V1");
    public static readonly SomeEnum V2 = new SomeEnum("V2");

    SomeEnum(String name) : base(name) {

    }
}

public class OtherEnum : AbstractEnum<OtherEnum> {

    public static readonly OtherEnum V1 = new OtherEnum("V1");
    public static readonly OtherEnum V2 = new OtherEnum("V2");

    OtherEnum(String name) : base(name) {

    }
}

This looks good and more or less does the trick... except that, following the letter of the spec, the actual instances (SomeEnum.V1, OtherEnum.V1 etc.) don't get initialized unless at least one of them is referred to explicitly. Static fields/methods in the base class don't count. So, for instance, the following:

Console.WriteLine("Count: {0}", SomeEnum.Values.Count());
foreach (SomeEnum e in SomeEnum.Values) {
    Console.WriteLine(e.Name);
}

writes Count: 0, but if I add the following line --

Console.WriteLine("SomeEnum.V1: " + SomeEnum.V1.Name);

-- even after the above, I get:

Count: 2
V1
V2

(Note, by the way, that initializing the instances in a static constructor makes no difference.)

Now, I can fix this by marking nameRegistry as protected and pushing Values and ValueOf down into the subclasses, but I was hoping to keep all the complexity in the superclass and keep the boilerplate to a minimum. Can anyone whose C#-fu is superior to mine come up with a trick for making the subclass instances "self-executing"?


Note: FWIW, this is in Mono, on Mac OS. YM in MS .NET, on Windows, MV.


ETA: For monoglot C# developers (or even polyglot developers whose experience is limited to languages starting with 'C') wondering WTF I'm trying to do: this. C# enums take care of the type safety issue, but they're still missing everything else.

Community
  • 1
  • 1
David Moles
  • 48,006
  • 27
  • 136
  • 235

4 Answers4

2

I came up with this - not entirely pleasing, but does do the job:

        public static IEnumerable<T> Values
        {
            get
            {
                if (nameRegistry.Count > 0)
                {
                    return nameRegistry.Values;
                }
                var aField = typeof (T).GetFields(
                                        BindingFlags.Public | BindingFlags.Static)
                                    .FirstOrDefault();

                if (aField != null)
                    aField.GetValue(null);

                return nameRegistry.Values;
            }
        }

EDIT Here's a slightly different version that should address VinayC's concerns in the comments. The problem was this: thread A calls Values(). While the static constructor of SomeEnum is running, after it's added V1 but before it adds V2, thread B calls values. In the code as originally written, it would be handed an IEnumerable that might only yield V1. So you could get incorrect results from Values() if a second thread calls during the very first call to Values() for any particular type.

The version below uses a boolean flag rather than relying on a non-zero count in nameRegistry. In this version it is still possible that the reflection code to run more than once, but no longer possible to get wrong answers from Values(), since by the time the reflection code completes, the nameRegistry is guaranteed to be fully initialized.

        private static bool _initialized;
        public static IEnumerable<T> Values
        {
            get
            {
                if (_initialized)
                {
                    return nameRegistry.Values;
                }
                var aField = typeof(T).GetFields(
                                            BindingFlags.Public | BindingFlags.Static)
                                        .FirstOrDefault();
                if (aField != null)
                    aField.GetValue(null);
                _initialized = true;
                return nameRegistry.Values;
            }
        }
Bryn Keller
  • 969
  • 6
  • 14
  • 1
    @David, IMO, there can be an issue here - consider two threads invoking values simultaneously (for first time) - the second thread may get a wrong count! – VinayC Mar 01 '11 at 05:12
  • Interesting point. I don't want locking overhead every time somebody accesses one of the static methods... but maybe locking the reflection code above, plus the guts of the constructor, would do the trick? Have to think about that and see where the race might be. – David Moles Mar 01 '11 at 05:16
  • @VinayC I'm not so sure in this case - remember the V1, V2, etc. are static fields, and IIRC the spec ensures that the static constructor only runs once. When you call Values initially, the problem is you haven't actually touched SomeEnum yet. As soon as you do (using the reflection code), the static constructor runs, which is threadsafe. Also consider that if you add a static Values property to SomeEnum that simply passes through to AbstractEnum.Values, that also works, but the goal was to avoid having to write more code in the "enum" classes. – Bryn Keller Mar 01 '11 at 05:24
  • @Xoltar, reflection code will trigger static constructor for `SomeEnum` and hence reflection code will halt till V1, V2 etc are added to dictionary. But `nameRegistry` is a static filed from `AbstractEnum` and access to it won't be blocked. So its possible for some thread to access it when it is getting modified by `SomeEnum` static constructor. – VinayC Mar 01 '11 at 06:55
  • @VinayC Agreed. So there's a case where the reflection code can be called more than once if there are several threads that call Values before they ever deal with a particular instance and before the very first call to Values or instance access has completed. It depends on your application, of course, but that seems like a pretty acceptable risk. In no case will Values return different results, the only danger is that you might run the reflection code more than once. – Bryn Keller Mar 01 '11 at 07:08
  • 1
    @Xoltar, Values will indeed return different results or to be correct, it will return `IEnumerable` to a mutating data - consider thread A has invoked `Values` triggering first `AbstractEnum` static constructor and then `SomeEnum` static constructor. Consider that while `SomeEnum` static constructor is running, thread B invokes Values - if at this stage, V1 has been added to `nameRegistry` then Values.Count will return 1, now thread B comes and say if V2 is also been added, it will get count = 2. – VinayC Mar 01 '11 at 08:04
  • @VinayC, Ah! I see what you mean now. I've updated the code sample to cover this condition, please take a look. Thanks for your help! – Bryn Keller Mar 01 '11 at 17:44
1

Admittedly, I don't know what RichEnums are, but does this C# not do what you want?

public enum SomeEnum
{
    V1,
    V2
}

class Program
{
    static void Main(string[] args)
    {
        var values = Enum.GetValues(typeof (SomeEnum));
        Console.WriteLine("Count: {0}", values.Length);
        foreach (SomeEnum e in values)
        {
            Console.WriteLine(e);
        }
    }
}
Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
  • It stops works the first time you need to add behavior to SomeEnum. See e.g. http://stackoverflow.com/questions/1376312/whats-the-equivalent-of-javas-enum-in-c – David Moles Mar 01 '11 at 04:58
  • I see. That's an interesting feature. I'm not sure I'd know how to use it well. – Ritch Melton Mar 01 '11 at 05:05
  • 1
    You can get simple behaviors using extension methods on SomeEnum. Of course that doesn't work for everything, for example no operator overloading. – irritate Mar 01 '11 at 05:17
  • Oh thats true. You just end up with named methods, which I think is Java-esque anyway. – Ritch Melton Mar 01 '11 at 05:19
1

How about:

public class BaseRichEnum 
{
   public static InitializeAll()
   {
      foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
      {
        if (t.IsClass && !t.IsAbstract && typeof (BaseRichEnum).IsAssignableFrom(t))
        {
          t.GetMethod("Initialize").Invoke(null, null); //might want to use flags on GetMethod
        }
      }
   }
}

public class AbstractEnum<T> : BaseRichEnum where T : AbstractEnum<T>
{
    static readonly IDictionary<String, T> nameRegistry = new Dictionary<String, T>();

    readonly String name;

    protected AbstractEnum (String name)
    {
        this.name = name;
        nameRegistry[name] = (T) this;
    }

    public String Name {
        get {
            return name;
        }
    }

    public static T ValueOf(String name) {
        return nameRegistry[name];
    }

    public static IEnumerable<T> Values {
        get {
            return nameRegistry.Values;
        }
    }    
}

And then:

public class SomeEnum : AbstractEnum<SomeEnum> 
{

        public static readonly SomeEnum V1;
        public static readonly SomeEnum V2;

        public static void Initialize()
        {
          V1 = new SomeEnum("V1");
          V2 = new SomeEnum("V2"); 
        }

        SomeEnum(String name) : base(name) {
        }
    }

Then you have to call BaseRichEnum.InitializeAll() in application startup code. I think it's better to impose this simple requirement on clients, thereby making visible the mechanism, than to expect future maintainers to grasp the subtleties of static-time initialization.

0

I don't like below solution as such but...

public class AbstractEnum<T> where T : AbstractEnum<T>
{
   ...
   private static IEnumerable<T> ValuesInternal {
        get {
            return nameRegistry.Values;
        }
   }

   public IEnumerable<T> Values {
     get {
       return ValuesInternal;
     }
   }
}

You have to use like SomeEnum.V1.Values - I know it sucks! Yet another alternative that would involve some work is

public class AbstractEnum<T> where T : AbstractEnum<T>
{
   ...
   protected static IEnumerable<T> ValuesInternal {
        get {
            return nameRegistry.Values;
        }
   }
}

public class SomeEnum : AbstractEnum<SomeEnum> {
  ...

  public static IEnumerable<SomeEnum> Values
  {
    get
    {
        return ValuesInternal;
    }

  }
}

I would go with the second option.

VinayC
  • 47,395
  • 5
  • 59
  • 72