mercredi 13 janvier 2016

Cannot Convert between Expression

A method I am trying to call (third party, can't change) expects a Func<TResult>. Using reflection I am trying to make an e.g. Func<Task<IEnumerable<Person>>> but I am inadvertently creating an Expression<Func<Task<IEnumerable<Person>>>>. The reason for reflection is that person is arbitrary and could be any type depending on how/when this is called.

// trying to mimic this hard-code: () => TaskEx.FromResult(Enumerable.Empty<Person>())
//var enumerableType = typeof(Person); //potentially

var empty = (typeof(Enumerable)).GetMethod("Empty");
var genereicEmpty = empty.MakeGenericMethod(enumerableType);
var emptyEnumerable = genereicEmpty.Invoke(null, null);

var fromResult = typeof (TaskEx).GetMethod("FromResult");
var genericFromResult = fromResult.MakeGenericMethod(genereicEmpty.ReturnType);

var enumerableTask = genericFromResult.Invoke(null, new [] {emptyEnumerable});

var functype = typeof(Func<>).MakeGenericType(genericFromResult.ReturnType);
var body = Expression.Constant(enumerableTask);
var lambdaType = typeof(Expression).GetMethods()
    .Where(x => x.Name == "Lambda")
    .Where(x => x.GetParameters().Length == 2)
    .Where(x => x.GetParameters()[1].ParameterType == typeof(ParameterExpression[]))
    .Single(x => x.IsGenericMethod);
var genericLambdaType = lambdaType.MakeGenericMethod(functype);
var lambda = genericLambdaType.Invoke(null, new object[] { body, new ParameterExpression[0] });

When I use lambda later I get the exception

Exception thrown: 'System.ArgumentException' in mscorlib.dll

Additional information: Object of type 'System.Linq.Expressions.Expression`1[System.Func`1[System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[DC.Dataloader.Models.Person]]]]' cannot be converted to type 'System.Func`1[System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[DC.Dataloader.Models.Person]]]'.

Is there a better way than Expression.Lambda to build a Func<> on the fly? I can't seem to find the correct way to do this.

Thank you





Aucun commentaire:

Enregistrer un commentaire