c# - Error passing both a dynamic object and an Action to a function -


i trying pass dynamic object and action function. (below simple test) but, getting following compile time error:

cannot use lambda expression argument dynamically dispatched operation without first casting delegate or expression tree type.

one or other ok... not both... help?

    void test()     {         dynamic obj = new system.dynamic.expandoobject();         obj.a = 1;         obj.b = 2;         calc(obj, (result) =>         {             console.write("result: " + result);         });         }      void calc(dynamic obj, action<int> oncomplete)     {         oncomplete((int)obj.a + (int)obj.b);     } 

sure - compiler says - cast lambda expression concrete type:

calc(obj, (action<int>)(result => console.write("result: " + result))); 

the reason have lambda expression doesn't have type - compiler has know delegate (or expression tree) type you're trying convert to. can't if method you'll calling won't chosen until execution time, case when argument dynamic.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -