在试验了迭代器块之后,我注意到生成的IL代码并不是我期望的那样。不是生成一个try finally块,而是生成一个try故障块,这是我从未见过的。我注意到编译器不允许我在“手写”C_中使用fault关键字。
二者有什么区别吗?
C代码:
static IEnumerable<string> ReadAllLines(string fileName)
{
using (var file = System.IO.File.OpenText(fileName))
{
string s;
while ((s = file.ReadLine()) != null)
{
yield return s;
}
}
}
MSIL代码:
.method private hidebysig newslot virtual final instance bool MoveNext() cil managed
{
.override [mscorlib]System.Collections.IEnumerator::MoveNext
.maxstack 3
.locals init (
[0] bool CS$1$0000,
[1] int32 CS$4$0001,
[2] string CS$0$0002,
[3] bool CS$4$0003)
L_0000: ldarg.0
// try body
L_008d: leave.s L_0097
L_008f: ldarg.0
L_0090: call instance void ConsoleApplication2.Program/<ReadAllLines>d__0::System.IDisposable.Dispose()
L_0095: nop
L_0096: endfinally
L_0097: nop
L_0098: ldloc.0
L_0099: ret
.try L_0000 to L_008f fault handler L_008f to L_0097
}
有趣的一行是IL的最后一行,其中指定了一个错误处理程序,在普通的try finally块中指定了一个finally处理程序。