我不明白为什么编译器认为我的 dto 在 lambda 表达式中是动态的。这是一个错误还是有正当理由?
[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'"));
}
}