简体   繁体   中英

Initialization of static fields in C# generic types

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.

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;
            }
        }

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);
        }
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM