我不明白爲什麼編譯器認爲我的dto在lambda表達式內是動態的。 這是一個錯誤或有一個有效的理由?動態與lambda的奇怪行爲。 c#.net 4
[TestFixture]
public class DynamicTest
{
public class Dto
{
public string Value { get; set; }
}
public Dto ToDto(dynamic d)
{
return new Dto();
}
[Test]
public void dto_is_typed()
{
// var is Dto
var dto = ToDto(new { dummy = true });
dto.Value = "val";
Assert.Inconclusive("yust for intellisense test");
}
[Test]
public void dto_is_dynamic_inside_an_action_with_dynamic_type()
{
Action<dynamic> act = o =>
{
// dto is dynamic
var dto = ToDto(o);
dto.ThisIsNotAProperty = 100;
};
var ex = Assert.Throws<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() =>
{
act(new {dummy = true});
});
Assert.IsTrue(ex.Message.EndsWith("does not contain a definition for 'ThisIsNotAProperty'"));
}
}
你的問題沒有意義。你是否收到錯誤信息?你的測試失敗了嗎?你期望它做什麼?是什麼讓你認爲「編譯器認爲你的動態?」 – 2013-05-03 10:08:13
測試通過。我沒有編譯異常,但運行時異常 – 2013-05-03 10:12:25
什麼是運行時異常?它引發異常的代碼是什麼? – 2013-05-03 10:22:46