The code to create the expression dynamically would be like this:
ParameterExpression parameter = Expression.Parameter(typeof (GoalsModelUnitOfWork), "i");
MemberExpression property = Expression.Property(parameter, "AcademicCycles");
var queryableType = typeof (IQueryable<>).MakeGenericType(typeof (AcademicCycle));
var delegateType = typeof (Func<,>).MakeGenericType(typeof (GoalsModelUnitOfWork), queryableType);
var yourExpression = Expression.Lambda(delegateType, property, parameter);
The result will have the desired type, but the problem is that the return type of Expression.Lambda()
is LambdaExpression
and you can’t perform a type cast to Expression<Func<...>>
to pass it as parameter to your setup function because you don’t know the generic type parameters for the Func
. So you have to invoke the Setup
method by reflection, too:
this.GetType().GetMethod("Setup", yourExpression.GetType()).Invoke(this, yourExpression);