[c#] Windows Phone上的無功擴展錯誤


Answers

Question

編譯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());
    }
}



Links