2

When I run:

MethodInfo m = typeof(Expression).GetMethod("Lambda", new Type[]{typeof(Expression), typeof(ParameterExpression[])});

I get:

System.Reflection.AmbiguousMatchException: Ambiguous match found.

This is in an effort to do this genericly (instead of knowing it's a string)

var newExpression = Expression.Lambda<Func<T, string>>(propertyExpression, parameters);
return entities.OrderBy(newExpression);

EDIT: Additional info:

running:

typeof(Expression).GetType().GetMethod("GetMethodCandidates", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(typeof(Expression),new object[]{"Lambda", BindingFlags.Static| BindingFlags.Public, CallingConventions.Standard,new Type[]{typeof(Expression), typeof(ParameterExpression).MakeArrayType()},false})

returns the array:

[0]: {System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])}
[1]: {System.Linq.Expressions.Expression`1[TDelegate] Lambda[TDelegate](System.Linq.Expressions.Expression, System.Collections.Generic.IEnumerable`1[System.Linq.Expressions.ParameterExpression])}
[2]: {System.Linq.Expressions.LambdaExpression Lambda(System.Linq.Expressions.Expression, System.Linq.Expressions.ParameterExpression[])}
[3]: {System.Linq.Expressions.LambdaExpression Lambda(System.Linq.Expressions.Expression, System.Collections.Generic.IEnumerable`1[System.Linq.Expressions.ParameterExpression])}
Charlie
  • 8,530
  • 2
  • 55
  • 53
  • If you use typeof(Expression).GetMethods instead does it find more than one, and how do they differ? – Tony Hopkinson Aug 09 '12 at 22:29
  • I get what amounts to the list of methods inside the MSDN docs. The only thing that had any indication was a generic and non generic version of the method. But when I used the MakeGenericType() I didn't get static methods back, which is what I needed. – Charlie Aug 10 '12 at 02:29
  • 1
    possible duplicate of [Select Right Generic Method with Reflection](http://stackoverflow.com/questions/3631547/select-right-generic-method-with-reflection) – nawfal Jan 18 '14 at 05:48

1 Answers1

2

Unfortunately reflection was written before generics were made available and often doesn't support generic methods/classes well at all.

Similar questions to this have been asked (GetMethod for generic method) where the answer has been to get the candidate methods and pick the one you want from the collection of methods.

Community
  • 1
  • 1
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98