I have method that looks something like
ISomething<U> Foo<T, U>(Expression<Func<T, U>> selector)
{
Expression<Func<T, object>> generalSelector =
ChangeSelectorReturnType<object>(selector);
Use(generalSelector);
return new Something<U>(selector);
}
What is the simplest code to implement ChangeSelectorReturnType assuming selector will always be a simple property accessor such as x => x.Property?
I know the solution presented in another question works, but it requires a full expression visitor because no assumptions are made. I’m ok with assumptions in this case.
Use the
Convert()expression on the body of the lambda. That will allow you to change its type. Then recreate the lambda.