Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

380
Views
Can not pass dynamic argument and lambda to the method

Strange behavior of DLR. I have a method accepts two arguments: dynamic and Func<>. When I pass only dynamic OR only Func<> - no errors. But when I try to pass these arguments at same time - appears error "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.":

    static void Main(string[] args)
    {
        dynamic d = 1;

        Method1(d);// - OK
        Method2(f => 1);// - OK
        Method3(d, f => 1);// - Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.
    }

    static void Method1(dynamic d)
    {
    }

    static void Method2(Func<string, int> func)
    {
    }

    static void Method3(dynamic d, Func<string, int> func)
    {
    }

Why it happens?

Of course I can make explicit casting, and error go away:

Method3(d, (Func<string, int>)(f => 1));

But it is uncomfortably. The compiler knows type of lambda, why it requires casting?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Have done some research and read through some of the IL generated by the compiler for each of your cases.

This seems to be a limitation in the flexibility of dynamic compilation. Because your method takes a dynamic parameter, the entire call, now becomes a dynamic operation. This means that all of the parameters are late bound, so the processing of the parameters goes through a different processing path during compilation than for parameters not participating in a dynamic operation.

Clearly, as your call to Method2 demonstrates, the compiler has the ability to infer that your intent is for f => 1 to be treated as a Func<string,int>.

However, it looks like this functionality, probably because of the complexity of building out the dynamic call-site, is not yet supported in dynamic compilation.

This is one of those cases where Microsoft is not yet supporting a feature, but may add it in the future.

For now it looks like you have no choice but to give the compiler a hint.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!