0

I want to do exactly the same thing but with Where (not Add). Visual Studio told me to cast it in delegate or in tree type, but I'm stuck with that, when I try to cast it, it doesn't work. Can somebody gives me a short example of how to cast the lambda expression in delegate ?

Thank you!

Sorry for late edit.

[AllowAnonymous]
public ActionResult test()
{
    Contexte contexte = new Contexte();
    var listField = contexte.GetType().GetField("<offreStages>k__BackingField", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    dynamic fldVal = listField.GetValue(contexte);



    fldVal.Where(os => os.id == 1);
    /* 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.
    */

    return View();
}

From this post, it says we can use dynamic to directly call method without complicated Invoke, but as you can see, it doesn't work if we need to send a lambda expression and I just don't understand the error (how to cast it). Can somebody explain it and give me an example ?

Community
  • 1
  • 1
Sproulx
  • 314
  • 3
  • 13
  • 1
    Welcome to Stack Overflow. Please provide a [mcve] of the problem - it's really hard to help when we've only get a vague description of what you're trying to do, and no indication of what's going wrong beyond "it doesn't work". – Jon Skeet Nov 23 '15 at 21:12
  • Please show us what you are attempting to do, with *your* code. – Ron Beyer Nov 23 '15 at 21:14
  • 1
    http://stackoverflow.com/questions/1452261/how-do-i-invoke-an-extension-method-using-reflection – TyCobb Nov 23 '15 at 21:51
  • Based on what little information you've provided, I'm _guessing_ that you want to use `typeof(System.Linq.Enumerable)` instead of calling `GetType()` on an object instance, call `GetMethod("Where")` (specifying appropriate parameter types to get the overload you want), and then invoke that `MethodInfo` as would otherwise be used in the other examples you've seen. But it's impossible to know for sure, never mind exactly what that would look like without [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) showing what you're trying to do. – Peter Duniho Nov 23 '15 at 22:39
  • 1
    Please do not post screenshots of code, or if you really need to also include a text copy of the code. – Scott Chamberlain Nov 24 '15 at 16:07
  • @ScottChamberlain I'll do that in future post, thanks for the tip ! :) – Sproulx Nov 24 '15 at 16:27
  • How about do it in this post... – Scott Chamberlain Nov 24 '15 at 16:28
  • @ScottChamberlain Clearly not here today sorry, edited with code sample and error from visual studio. – Sproulx Nov 24 '15 at 17:31

1 Answers1

0

Generally spoken, the compiler is confused about the type of the lambda expression. Either cast it or use a constuctor to clarify. But in this case Where is in fact an extension method to IEnumerables, defined in the class Enumerable (note the difference!). Change your code as follows:

Contexte contexte = new Contexte();
var listField = contexte.GetType().GetField("<offre_Stages>_k__BackingField", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// either:
IEnumerable<OS> fldVal = (IEnumerable<OS>)listField.GetValue(contexte); // don't use ``dynamic`` as we know it's an ``IEnumerable`1``
fldVal.Where(os => os.id == 1);
// or:
dynamic fldVal = listField.GetValue(contexte);
Enumerable.Where((IEnumerable<OS>)fldVal, os => os.id == 1); // explicit call to the extension method
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29