1

I have a lambda expression tree issue that I can’t figure out. I am trying to make a dynamic linq Select statement.

I have a dynamic repository created here:

private static dynamic GetRepository(Type type)
{
    dynamic repository = typeof(IFactory).GetMethod("Create").MakeGenericMethod(typeof(IRepository<>).MakeGenericType(type)).Invoke(ObjectFactory.Instance, new object[] { });
    return repository;
}

With this I need to call this only I don’t know x and SomeProperty at compile time. I have PropertyInfo propertyInfo with SomeProperty name and Type objectType with x type. It fails at Goal 1 with this exception:

System.Reflection.AmbiguousMatchException at GetMethod(string name)

The code:

private SomeObject CreateSomeObject (PropertyInfo propertyInfo, Type objectType)
{
    var param = Expression.Parameter(objectType, "x");
    MemberExpression expression = Expression.PropertyOrField(param, propertyInfo.Name);

    //Goal 1: var selectExpression = Expression.Lambda<Func<objectType, object>>(expression, param);
    var selectExpression = typeof(Expression).GetMethod("Lambda").MakeGenericMethod(typeof(Func<,>)
    .MakeGenericType(objectType, typeof(object)))
    .Invoke((object)null, new object[] { expression, param });

    // Goal 2: List<object> list = GetRepository(objectType).FindAllQuery().Select(x => x.SomeProperty).ToList();
    List<object> list = GetRepository(objectType).FindAll().Select(selectExpression);
}

How to solve this?

Update 1:

I have changed the way to select Lambda method, the way to pack 'param' parameter and I have added a object Converter to 'expression'.

private SomeObject CreateSomeObject (PropertyInfo propertyInfo, Type objectType)
{
    var param = Expression.Parameter(objectType, "x");
    Expression expression = Expression.Convert(Expression.PropertyOrField(param, propertyInfo.Name), typeof(object));

    //Goal 1: var selectExpression = Expression.Lambda<Func<objectType, object>>(expression, param);
    var selectExpression = typeof(Expression).GetMethods().First(m => m.Name == "Lambda" && m.IsGenericMethod)
    .MakeGenericMethod(typeof(Func<,>)
    .MakeGenericType(objectType, typeof(object)))
    .Invoke((object)null, new object[] { expression, new [] { param }});

    // Goal 2: List<object> list = GetRepository(objectType).FindAllQuery().Select(x => x.SomeProperty).ToList();
    List<object> list = GetRepository(objectType).FindAll().Select(selectExpression);
}

But know I get this exception at Goal 2(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException):

'System.Collections.Generic.List ' does not contain a definition for 'Select'

This is partly right because it is defined in System.Linq and it is an extension method. How do I get this working?

Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85

1 Answers1

3

The code that throws the exception is

typeof(Expression).GetMethod("Lambda")

because there are 18 methods called Lambda defined on the Expression type (thus the AmbiguousMatchException).

GetMethod(string methodName) is appropriate when there are no overloads. In this case I would use GetMethods() and then filter out the one I need.

In your case, the right overload is

Expression.Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters)

You could write a function that validates the right overload by checking the number of parameters and their type, but I found an easier alternative: filter the method by the .ToString() representation, which in our case is:

System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])

There's also a problem with the way you pass the parameters (new object[] { expression, param }). The second parameter is not of type ParameterExpression, but ParameterExpression[] (array), therefore you should pass new[]{param} instead of just param. When calling it in regular code it works like that because it's defined as params ParameterExpression[].

In conclusion, the following code should work in your case:

const string methodSignature = 
    "System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate]" +
    "(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])";

var lambdaMethod = typeof (Expression).GetMethods()
    .Single(mi => mi.ToString() == methodSignature);

var funcType = typeof (Func<,>).MakeGenericType(objectType, typeof (object));

var genericLambda = lambdaMethod.MakeGenericMethod(funcType);

var selectExpression = genericLambda.Invoke(null, new object[] { expression, new[] { param } });
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
  • Actually [there are 18 methods called `Lambda` on `Expression`](http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.lambda.aspx) and 6 of them are generic. So, your code will return one of the generic ones, but I think it most likely won't be the right one. – svick Sep 20 '12 at 10:36
  • Thanks.. Now I get: A object of the type 'System.Linq.Expressions.TypedParameterExpression' can’t be converted to the type 'System.Linq.Expressions.ParameterExpression[]'. At the Invoke method. – Morten Holmgaard Sep 20 '12 at 10:37
  • Please see the update in the original post - any solution on that exception? – Morten Holmgaard Sep 20 '12 at 12:51
  • @MortenHolmgaard the `.First(m => m.Name == "Lambda" && m.IsGenericMethod)` condition is wrong, as @svick pointed out above. Please see if the update to my answer solves your problem. – Cristian Lupascu Sep 20 '12 at 13:01
  • I have tried your new solution, and just as in my 'Update 1' this seams to work but I den get an exception when calling 'Select'.. I get: 'System.Collections.Generic.List ' does not contain a definition for 'Select'.. Se my update.. – Morten Holmgaard Sep 20 '12 at 13:21
  • @MortenHolmgaard That's because extension methods don't work on `dynamic`. But I'm not sure how exactly to fix that in your case. – svick Sep 20 '12 at 13:53
  • @MortenHolmgaard if you do `.Select(x => x.SomeProperty)`) instead of `.Select(selectExpression)`, does that work? – Cristian Lupascu Sep 20 '12 at 14:25
  • No I can't because of dynamic.. I have tried som invoke of the extension method with `var queryList = typeof(Enumerable).GetMethods().First(m => m.Name == "Select").MakeGenericMethod(objectType, typeof(object)).Invoke(null, new[] { _repository.FindAll(), selectExpression });` but it throws ArgumentException.. I have just for now solved it the old way, but I am still interested in a solution: `IList allValues = new List(); foreach (var obj in _repository.FindAll()) allValues.Add((double)ReflectionHelper.GetProperty(propertyInfo.Name, obj));` – Morten Holmgaard Sep 20 '12 at 14:55