7

我已经浏览了 C# Language Spec (v5.0) 的相关部分,但我找不到与我所看到的内容相关的部分。

如果你运行下面的代码,你会看到下面的输出,这是我所期望的:

using System;

class Test {
   static int count = 0;
   static void Main() {
      Console.WriteLine("In Main(), A.X=" + A.X);
   }

   public static int F(string message) {
      Console.WriteLine(message);
      A.X = ++count;

      Console.WriteLine("\tA.X has been set to " + A.X);
      B.Y = ++count;

      Console.WriteLine("\tB.Y has been set to " + B.Y);
      return 999;
   }
}
class A {
   static A() { }
   public static int U = Test.F("Init A.U");
   public static int X = Test.F("Init A.X");
}

class B {
   static B() { }
   public static int R = Test.F("Init B.R");
   public static int Y = Test.F("Init B.Y");
}

输出是:

Init A.U
    A.X has been set to 1
Init B.R
    A.X has been set to 3
    B.Y has been set to 4
Init B.Y
    A.X has been set to 5
    B.Y has been set to 6
    B.Y has been set to 2
Init A.X
    A.X has been set to 7
    B.Y has been set to 8
In Main(), A.X=999

这正是我所期望的输出。特别注意,即使方法 F() 使用参数“Init AU”执行,一旦遇到对 BY 的引用,它也会再次调用(如果您愿意,可以中断),导致 B 的静态初始化程序执行。一旦 B 的静态构造函数完成,我们再次返回到 F() 的 AU 调用,这说明 BY 设置为 6,然后设置为 2。所以,希望这个输出对每个人都有意义。

这是我不理解的:如果您注释掉 B 的静态构造函数,这是您看到的输出:

Init B.R
        A.X has been set to 1
        B.Y has been set to 2
Init B.Y
        A.X has been set to 3
        B.Y has been set to 4
Init A.U
        A.X has been set to 5
        B.Y has been set to 6
Init A.X
        A.X has been set to 7
        B.Y has been set to 8
In Main(), A.X=999

C# 规范 (v5.0) 的第 10.5.5.1 和 10.12 节指示 A 的静态构造函数(及其静态初始化程序)在“引用类的任何静态成员”时触发执行。然而,这里我们从 F() 中引用了 AX,并且没有触发 A 的静态构造函数(因为它的静态初始化程序没有运行)。

由于 A 有一个静态构造函数,我希望这些初始化程序运行(并中断)对 F() 的“Init BR”调用,就像 B 的静态构造函数在我展示的“Init AU”调用中中断 A 对 F() 的调用一样一开始。

谁能解释一下?从表面上看,它看起来违反了规范,除非规范的其他部分允许这样做。

谢谢

4

1 回答 1

2

我想我明白这里发生了什么,尽管我没有很好的解释为什么会这样。

测试程序有点太粗略,看不出发生了什么。让我们做一个小调整:

class Test {
   static int count = 0;
   static void Main() {
      Console.WriteLine("In Main(), A.X=" + A.X);
   }

   public static int F(string message) {
       Console.WriteLine("Before " + message);
       return FInternal(message);
   }

   private static int FInternal(string message) {
      Console.WriteLine("Inside " + message);
      A.X = ++count;

      Console.WriteLine("\tA.X has been set to " + A.X);
      B.Y = ++count;

      Console.WriteLine("\tB.Y has been set to " + B.Y);
      return 999;
   }
}
class A {
   static A() { }
   public static int U = Test.F("Init A.U");
   public static int X = Test.F("Init A.X");
}

class B {
   static B() { }
   public static int R = Test.F("Init B.R");
   public static int Y = Test.F("Init B.Y");
}

输出类似于问题中的输出,但更详细:

Before Init A.U  
Inside Init A.U  
    A.X has been set to 1  
Before Init B.R  
Inside Init B.R  
    A.X has been set to 3  
    B.Y has been set to 4  
Before Init B.Y  
Inside Init B.Y  
    A.X has been set to 5  
    B.Y has been set to 6  
    B.Y has been set to 2  
Before Init A.X  
Inside Init A.X  
    A.X has been set to 7  
    B.Y has been set to 8  
In Main(), A.X=999

这里没有什么令人惊讶的。删除 B 的静态构造函数,这就是你得到的:

Before Init A.U  
Before Init B.R  
Inside Init B.R  
    A.X has been set to 1  
    B.Y has been set to 2  
Before Init B.Y  
Inside Init B.Y  
    A.X has been set to 3  
    B.Y has been set to 4  
Inside Init A.U  
    A.X has been set to 5  
    B.Y has been set to 6  
Before Init A.X  
Inside Init A.X  
    A.X has been set to 7  
    B.Y has been set to 8  
In Main(), A.X=999

现在这很有趣。我们可以看到原始输出具有误导性。我们实际上是从尝试初始化A.U. 这并不奇怪,因为 A 应该首先被初始化,因为A.X它是在 Main 中访问的。下一部分很有趣。看起来当 B 没有静态构造函数时,CLR在进入方法之前FInternal中断了将要访问 B 的字段 ( )的方法。将此与另一种情况进行对比。在那里,B 的初始化被延迟,直到我们真正访问 B 的字段。

我不完全确定为什么事情会按照这个特定的顺序完成,但是你可以看到 B 的初始化没有被中断来初始化 A 的原因是 A 的初始化已经开始了。

于 2015-04-18T21:02:17.467 回答