考虑以下代码:

public class CountEnum : EnumDataType
    {
        public static readonly CountEnum CANADA = new CountEnum("CA", "Canada");
        public static readonly CountEnum DEFAULT = new CountEnum();
        private static readonly EnumDataTypeList OPTIONS = new EnumDataTypeList();
        public static readonly CountEnum UNITED_STATES = new CountEnum("US", "United States");
        public static readonly CountEnum UNSET = new CountEnum();


        private CountEnum()
        {
        }

        private CountEnum(string code, string name)
        {
            base.code = code;
            base.name = name;
            OPTIONS.Add(this); // This is the line 23
        }


以下是异常详细信息:

System.TypeInitializationException was caught
  Message=The type initializer for 'CP.BusinessLogic.VConfig' threw an exception.
  Source=CP.BusinessLogic
  TypeName=CP.BusinessLogic.VConfig
  StackTrace:
       at CP.BusinessLogic.VConfig.get_Instance()
       at CP.Fac.CPFac.GetActs() in C:\tfs_src\TeamProject\Main\Source\DApp\CPApp\Fac\CPFac.cs:line 170
  InnerException: System.TypeInitializationException
       Message=The type initializer for 'CP.Types.CountEnum' threw an exception.
       Source=CP.BusinessLogic
       TypeName=CP.Types.CountEnum
       StackTrace:
            at CP.BusinessLogic.VConfig..ctor() in C:\tfs_src\TeamProject\Main\Source\DApp\CPApp\BusinessLogic\VConfig.cs:line 14
            at CP.BusinessLogic.VConfig..cctor() in C:\tfs_src\TeamProject\Main\Source\DApp\CPApp\BusinessLogic\VConfig.cs:line 11
       InnerException: System.NullReferenceException
            Message=Object reference not set to an instance of an object.
            Source=CP.DataObject
            StackTrace:
                 at CP.Types.CountEnum..ctor(String code, String name) in C:\tfs_src\TeamProject\Main\Source\DApp\CPApp\Types\CountEnum.cs:line 23
                 at CP.Types.CountEnum..cctor() in C:\tfs_src\TeamProject\Main\Source\DApp\CPApp\Types\CountEnum.cs:line 8
            InnerException:


我无法弄清楚为什么我在运行时遇到此异常。我没有任何构建错误,并且代码看起来正确。

最佳答案

从堆栈跟踪来看,我认为您已经错过了有罪代码。例如,也许实际上看起来像这样:

public class CountEnum
{
   public static readonly CountEnum DEFAULT = new CountEnum();
   private static readonly List<CountEnum> AllInstances = new List<CountEnum>();

   private CountEnum()
   {
       AllInstances.Add(this);
   }
}


这将表现出堆栈跟踪显示的方式,而CountEnum构造函数将抛出NullReferenceException。但是,由于您还没有包含CountEnum构造函数的主体(或者实际上根本没有发布CountEnum的有效代码),我们无法确定。

但是从根本上讲,这是堆栈跟踪的一部分:

InnerException: System.NullReferenceException
     Message=Object reference not set to an instance of an object.
     Source=CP.DataObject
     StackTrace:
          at CP.Types.CountEnum..ctor(String code, String name) in C:\...\CountEnum.cs:line 23
          at CP.Types.CountEnum..cctor() in C:\...\CountEnum.cs:line 8


您应该查看CountEnum.cs的第23行-这就是引发异常的原因。

编辑:好的,现在我们可以看到有问题的代码,这与我的猜测非常相似。看下面的代码:

public static readonly CountEnum CANADA = new CountEnum("CA", "Canada");
public static readonly CountEnum DEFAULT = new CountEnum();
private static readonly EnumDataTypeList OPTIONS = new EnumDataTypeList();


这些变量将按照您指定的顺序进行初始化-因此,对CountEnum("CA", "Canada")的调用将在OPTIONS具有非空值之前执行...并且当您尝试将其添加到OPTIONS时,以完全正常的方式获取异常。

您可以通过将OPTIONS的声明移到顶部来解决此问题:

private static readonly EnumDataTypeList OPTIONS = new EnumDataTypeList();
public static readonly CountEnum CANADA = new CountEnum("CA", "Canada");
public static readonly CountEnum DEFAULT = new CountEnum();


保证可以正常工作,但这是一个相当脆弱的设计。我过去曾使用过这种设计,但是当您需要特定的初始化顺序时,您可能会陷入混乱。

07-28 13:00