Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Last active December 16, 2015 22:39
Show Gist options
  • Select an option

  • Save andreabalducci/5508218 to your computer and use it in GitHub Desktop.

Select an option

Save andreabalducci/5508218 to your computer and use it in GitHub Desktop.
strange behavior in vs2012 + r#
[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'"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment