-
-
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
ExpressionParser failed to parse Func type #570
Labels
Comments
[Test]
public void Issue570()
{
var p1 = Expression.Parameter(typeof(Customer), "p1");
var config = new ParsingConfig
{
CustomTypeProvider = new FuncTypeProvider()
};
var lambda = DynamicExpressionParser.ParseLambda(config, new[] { p1 }, typeof(bool), "p1.Method().Invoke(1)");
var compiledLambda = lambda.Compile();
//Actually, you don't need dynamic invoke since the lambda is parsed
//and you know the actual type of your delegate
var staticInvoke = ((Func<Customer, bool>)compiledLambda)(new Customer());
TestContext.WriteLine($"Static invoke: {staticInvoke}");
//But if, however, you want dynamic invoke it works as well
var dynamicInvoke = compiledLambda.DynamicInvoke(new Customer());
TestContext.WriteLine($"Dymamic invoke: {dynamicInvoke}");
}
public class Customer
{
public Func<int, bool> Method()
{
return x => x > 0;
}
}
//Consider deriving form DefaultDynamicLinqCustomTypeProvider rather than implementing the interface
public class FuncTypeProvider : DefaultDynamicLinqCustomTypeProvider
{
public override HashSet<Type> GetCustomTypes()
{
var customTypes = base.GetCustomTypes();
customTypes.Add(typeof(Func<int, bool>));
return customTypes;
}
} |
Hello, @ademchenko |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was trying to parse a expression string like this:
And the
Customer
class looks like this:FuncTypeProvider
is implemented like this:Exception occurs when creating
e3
:System.Linq.Dynamic.Core.Exceptions.ParseException: 'Expression of type 'Func`2' expected'
Stack trace:
Is it because I missed something that should be configured before parsing? Or is it because the lib cannot process a
Func
type return?I'm quite a noob here, if there's anything wrong with my expression or the way I express, please let me know. Any help would be appreciated.
The text was updated successfully, but these errors were encountered: