C# 编译器错误?表达式中用于只写属性的对象初始值设定项语法导致 csc 崩溃
您可能会认为这是一个错误报告,但是我很好奇我在这里是否犯了严重错误,或者 Eric 或 Microsoft 的其他人是否有解释。
更新
现在是在 Microsoft Connect 上发布为错误。
描述
考虑以下类:
class A
{
public object B {
set { }
}
}
这里,AB
是一个只写,但其他方面都很好。
现在,想象一下我们在表达式内部分配它:
Expression<Func<A>> expr =
() => new A {
B = new object { }
};
此代码使 C# 编译器(3.5.30729.4926 和 4.0.30319.1)吐出
内部编译器错误(地址 013E213F 处为 0xc0000005):罪魁祸首可能是“BIND”。
并崩溃。
但是,仅用构造函数 (( )
) 替换对象初始值设定项语法 ({ }
) 就可以很好地编译。
用于复制的完整代码:(
using System;
using System.Linq.Expressions;
class Test {
public static void Main()
{
Expression<Func<A>> expr =
() => new A {
B = new object { }
};
}
}
class A {
public object B { set { } }
}
是的,我在实际项目中确实做到了。)
You may consider this a bug report, however I'm curious if I am terribly wrong here, or if there is an explanation from Eric or someone else at Microsoft.
Update
This is now posted as a bug on Microsoft Connect.
Description
Consider the following class:
class A
{
public object B {
set { }
}
}
Here, A.B
is a write-only but otherwise fine property.
Now, imagine we assign it inside of expression:
Expression<Func<A>> expr =
() => new A {
B = new object { }
};
This code makes C# compiler (both 3.5.30729.4926 and 4.0.30319.1) spit out
Internal Compiler Error (0xc0000005 at address 013E213F): likely culprit is 'BIND'.
and crash.
However, merely replacing object initializer syntax ({ }
) with a constructor (( )
) compiles just fine.
Full code for reproduction:
using System;
using System.Linq.Expressions;
class Test {
public static void Main()
{
Expression<Func<A>> expr =
() => new A {
B = new object { }
};
}
}
class A {
public object B { set { } }
}
(And yes, I did hit it working on a real project.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕我不是 Eric Lippert(哦,但我能这么潇洒吗……),但作为一名仍然可以搜索源代码的前 Visual Studio 语言人员,我可以说两点关于此的事情:
任何时候你看到以“内部编译器错误”开头的东西,你肯定发现了一个错误。这就是该错误存在的原因,无论是 C#、VB 还是 C++ 编译器。这是“哦,糟糕,刚刚发生了出乎意料的错误!” throw-up-our-hands-and-bail-out 错误。
除此之外,这绝对是 C# 编译器中应该报告的错误。崩溃的代码假设当你对一个属性进行初始化时,它可以查看一个 getter,然后,嘿,你猜怎么着?在这种情况下,没有。奇怪的是,如果我将构造的类型更改为某种类型“C”而不是“对象”,我不会崩溃,所以我猜测这是堆栈上层的失败(即代码永远不应该得到) 除此之外,这绝对是 C# 编译器中
希望这有帮助。
I'm afraid I'm not Eric Lippert (Oh, but could I be so dashing...), but as a former Visual Studio languages guy who can still search the sources, I can say two things about this:
Any time you see something that starts with "Internal Compiler Error" you have most definitely found a bug. That's what that error exists for, whether it's the C#, VB or C++ compiler. It's the "Oh, s**t, something just went really unexpectedly wrong!" throw-up-our-hands-and-bail-out error.
Beyond that, this is definitely a bug in the C# compiler that should be reported. The code that's crashing is assuming that when you're doing an initializer on a property that there's a getter it can look at and, hey, guess what? In this case, there isn't. Oddly enough, if I change the type being constructed to some type "C" instead of "object", I don't get the crash, so I'm guessing it's a failure further up the stack (i.e. the code never should have gotten down to the point where it was looking for the property getter).
Hope this helps.
这是我在网上找到的与该错误相关的内容,
由 Microsoft 于 2010 年 3 月 9 日上午 10:58 发布
This is what I found online related to the error,
Posted by Microsoft on 3/9/2010 at 10:58 AM