Strange behavior with dynamics & lambda. c# .net 4 -


i cannot figure why compiler thinks dto dynamic inside lambda expression. it's bug or there's valid reason that?

[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 dto         var dto = todto(new { dummy = true });         dto.value = "val";          assert.inconclusive("yust intellisense test");     }      [test]     public void dto_is_dynamic_inside_an_action_with_dynamic_type()     {         action<dynamic> act = o =>             {                 // dto 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 definition 'thisisnotaproperty'"));     } } 

the fact it's in lambda expression irrelevant. you've got:

dynamic o = getvaluefromsomewhere(); var dto = todto(o); 

now type of dto dynamic, because expression todto(o) dynamic expression: you're using dynamic value (o) argument. though know there's 1 method ever resolve to, language rules force type of invocation expression dynamic.

you can change it, not using var:

action<dynamic> act = o => {     // dto dynamic     dto dto = todto(o);     dto.thisisnotaproperty = 100; }; 

this fail compile - assume wanted.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -