c# – 静态对象在正确的时间为空/未初始化

我正在使用这里概述的
type-safe enum pattern.我需要将一个类型安全的枚举嵌套到另一个中.在创建父构造函数时,子属性(静态对象)为NULL.似乎没有调用子构造函数,我遇到了一些错误.(父母和孩子我很困惑,但它解释了层次结构)

这是一个例子(我正在使用netMF):

public class MyDeviceSetting //parent
{
        public readonly string Name;
        public MyUnit SettingUnit;
        public readonly MyUnit.UnitPurpose UnitPurpose;

          #region MY STATIC SETTINGS
        //UNIT SETTINGS
        public static MyDeviceSetting TempUnits = new MyDeviceSetting("TempUnits", MyUnit.mm); //MyUnit.mm is null. Why?
        public static MyDeviceSetting BLAH = new MyDeviceSetting("BLAH", MyUnit.inch);//MyUnit.inch is null. Why?
          #endregion



        /// <summary>
        /// This is the MAIN PRIVATE Constructor
        /// </summary>
        /// <param name="?"></param>
        private MyDeviceSetting(string name, MyUnit defaultUnit)
        {
            Name = name;
            SettingUnit = defaultUnit;//NULL
            UnitPurpose = SettingUnit.Purpose; //fails because SettingUnit is NULL


        }


    }


public sealed class MyUnit
{
    private static int Count = 0;

    //these are used to store and identify the unit in memory
    public readonly int UnitID;
    public readonly int TestID;

    public enum UnitPurpose
    {
        DISTANCE,
        SPEED,
        TEMPERATURE,
        TIME,
        CLOCK,
        NO_UNITS
    }

    public readonly string DisplayName;
    public readonly string Abbreviation;
    public readonly string Name;
    public readonly UnitPurpose Purpose;

    #region My Units
    public static readonly MyUnit mm = new MyUnit("Milimeters", "mm", "mm", UnitPurpose.DISTANCE, 1);
    public static readonly MyUnit inch = new MyUnit("inch", "inch", "in", UnitPurpose.DISTANCE, 2);



    #endregion

    private MyUnit(string name,
                   string displayName,
                   string abbreviation,
                   UnitPurpose unitPurpose,
                   int unitID)
    {
        Name = name;
        DisplayName = displayName;
        Abbreviation = abbreviation;
        Purpose = unitPurpose;
        UnitID = unitID;
        TestID = Count;
        Count++;

    }


}

如何确保孩子不为空?有解决方法吗?编辑:This Post确保这应该工作,但在我的情况下,它不起作用.
《c# – 静态对象在正确的时间为空/未初始化》

最佳答案 这似乎是.Net Micro框架的错误/限制.它不完全支持静态构造函数.这是报告相同问题的人:
https://msdn.microsoft.com/en-us/library/Cc533032.aspx

NetCF 3.0的documentation包含以下警告:

Do not use static constructors. They are not yet fully supported by the .NET Micro Framework.

从这个blog post也似乎说(至少从2.0开始)对静态构造函数的调用是序列化的:

There are some things that cannot be done in .NET Compact Framework in
a static constructor which are possible in the full .NET Framework.
Basically, all static constructors when are executed in a serialized
fashion in .NET Compact Framework V2

该帖子在死锁的背景下讨论它,但我相信这是不起作用的原因.

不幸的是,这意味着你不能依赖静态并且必须自己处理初始化和锁定.这样的事情应该有效:

private static MyUnit inch;

public static MyUnit Inch
{
    get
    {
        if (inch == null)
            inch = new MyUnit("inch", "inch", "in", UnitPurpose.DISTANCE, 2);
         return inch;
    }
}

遗憾的是,这会失去静态构造函数给你的线程安全性.这很难修复,因为你不能依赖静态成员,因为我们已经看到你不能依赖它们被初始化.

点赞