[CSHARP-3580] LINQ3: Use Lambda expression to get MethodInfo Created: 17/Apr/21  Updated: 28/Oct/23  Resolved: 04/Oct/21

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: 2.14.0

Type: Task Priority: Unknown
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: CSHARP-2608

 Description   

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.

 



 Comments   
Comment by Robert Stam [ 17/Apr/21 ]

Done but not reviewed

Generated at Wed Feb 07 21:45:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.