使用VS 2012
编译,项目类型为WP 8.0
。如果没有附加调试器,则以下代码将失败。
不知何故,如果没有附加调试器,编译器的优化会破坏Crash()
内部的代码-请参见代码中的注释。
在Lumia 1520(8.1)和Lumia 630(8.0)上进行了测试。
有任何想法是为什么会发生这种情况吗?
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
Button.Tap += (sender, args) => new A<B, string>(new B(), "string").Crash();
}
}
public class B
{
public void Foo<T>(T val) { }
}
public class A<T1, T2> where T1 : B
{
private T1 _t1;
private T2 _t2;
public A(T1 t1, T2 t2)
{
_t2 = t2;
_t1 = t1;
}
public void Crash()
{
var obs = Observable.Return(_t2);
obs.Subscribe(result =>
{
//CLR is expecting T2 to be System.String here,
//but somehow, after passing through Observable
//T2 here is not a string, it's A<T1, T2>
new List<T2>().Add(result);
});
//Will run normally if commented
_t1.Foo(new object());
}
}
Observable.Return<T2>(_t2);
,而不是让编译器来决定类型。这里可能存在错误,虽然可能性很小。 - cwharris