C# Expressions use Base Class Virtual Methodinfo
If you write a Expression where you call a Virtual Method, the C# Expression Parser uses the MethodInfo from the BaseClass where System.Dynamic.Linq uses the MethodInfo from the Derived Class.
see issue: https://github.com/linq2db/linq2db/issues/3475
This will be fixed in linq2db, but ,aybe will also fail in other Linq Frameworks
@jogibear9988
- Do you have any lead where this should be fixed in the code ?
- Or maybe create a PR with unit tests?
If I understand correctly, the following code:
using System.Linq.Expressions;
using static System.Console;
using static System.Linq.Expressions.Expression;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Dynamic.Core;
Expression<Func<int?, string?>> expr = x => x.ToString();
WriteLine(
((MethodCallExpression)expr.Body).Method.DeclaringType
);
var selector = "ToString()";
var prm = Parameter(typeof(int?));
var parser = new ExpressionParser(new[] { prm }, selector, new object[] { }, ParsingConfig.Default);
var expr1 = parser.Parse(null);
WriteLine(
((MethodCallExpression)expr1).Method.DeclaringType
);
prints out:
System.Object
System.Nullable`1[System.Int32]
where both should have been the same.
yes
@jogibear9988 and @zspitz
I did try to find the specific changes in source code in (https://github.com/zzzprojects/System.Linq.Dynamic/blob/archived/Src/System.Linq.Dynamic/DynamicLinq.cs) and this library.
But finding a method and getting the base-type is done the same way.
And the reason System.Nullable`1[System.Int32] is returned in that example code, is because this code line:

And this is required.
So can you please provide some more details on why / how this should be fixed in this project?
Hello @jogibear9988,
Can you please check my last comment?
I think FindMethod should not return the Method it found, but the result of ".GetBaseDefinition()" of the Method it found. So you always return the MethodInfo TopMost in the Type hierarchy wich has it's own implementation.
see pull #630