Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3580

LINQ3: Use Lambda expression to get MethodInfo

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.14.0
    • Affects Version/s: None
    • Component/s: LINQ3
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Use lambda expressions to specify  methods for which we need to get the MethodInfo. This sometimes (but not always) makes for slightly longer lines than before, but it is much clearer than the previous approach.

      For example, where we used to find the MethodInfo for the Queryable.Where method like this:

       __where = new Func<IQueryable<object>, Expression<Func<object, bool>>, IQueryable<object>>(Queryable.Where).Method.GetGenericMethodDefinition();
      

      we now instead write:

       __where = ReflectionInfo.Method((IQueryable<object> source, Expression<Func<object, bool>> predicate) => source.Where(predicate));

      Note that ReflectionInfo.Method takes a single argument: a lambda expression.

      In this example the parameters to lambda expression are:

       (IQueryable<object> source, Expression<Func<object, bool>> predicate)

      and the body of the lamba expression is:

       source.Where(predicate)

      The body "looks" exactly like a "use" of the Queryable.Where method we want the MethodInfo for, because it IS a use. So it's obvious what method we are talking about. The MethodInfo itself can be extracted from the body of the lambda expression.

      A very similar approach can also be used to get PropertyInfos and ConstructorInfos.

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: