-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context lost in object initializer #294
Comments
There seems to be several factors leading to this fail. One of them is ExpressionPromoter no being able to promote Lambdas, I had to add this to an overriden ExpressionPromoter: if (expr is LambdaExpression le)
{
if (((LambdaExpression)le).ReturnType == type)
{
return expr;
}
if (le.ReturnType == type.GetUnderlyingType() && type.IsNullable())
{
// Boxing
var boxed = Expression.Convert(le.Body, type);
// Use Expression.Lambda to get back to strong typing
// TODO: We should make this workd with Func<,,>, Func<,,,> etc..., but as the library has no handling of multidmensional argument lambdas so there's no point in doing so now...
var delegateType = typeof(Func<,>).MakeGenericType(le.Parameters[0].Type, type);
return Expression.Lambda(delegateType, boxed, le.Parameters);
}
} I can't make this a PR now because...
|
Are both defined in ReturnType : Isn't there another property you could use? |
I think the replacement for ReturnType is lambdaExpression.Body.Type: // ReSharper disable once CheckNamespace
namespace System.Linq.Expressions
{
internal static class LambdaExpressionExtensions
{
public static Type GetReturnType(this LambdaExpression lambdaExpression)
{
#if !NET35
return lambdaExpression.ReturnType;
#else
return lambdaExpression.Body.Type;
#endif
}
}
} I'm currently adding this here: |
Thanks! I am currently working in a workaround for the issue in our project, will deal with the root cause when time permits, I've been reading through the code and have an idea on how to restructure the code so we can get rid of the custom aggregate parsing logic in favor of a generic handling of method calls (including extension methods). Having that there will help for sure. |
Sorry I got confused and mixed things (all these issues are very related to the fact that lambda/method call parsing is very ad-hoc and harcdoded to very specific cases). The initial issue reported here was that an expression could not contain more than one lambda with the same lambda argument. The reason seems to be that the whole library is single-lambda oriented, and that is a big handicap. After a lambda has been parsed, the context was not being restored for the rest of the parsing. This PR shows the failure: This PR has the fix: |
Thanks for reporting and fixing. Closing this one... |
After parsing firs argument in object initializer, context is lost:
The text was updated successfully, but these errors were encountered: