I’ve been teaching myself Expression Trees since yesterday and I’m having problems comparing two string values. I’ve made this test case that fails with the error:
No method 'Compare' on type 'System.String' is compatible with the supplied arguments.
Fails at run-time on the left = Expression.Call(
Type type = typeof(string);
Expression left, right;
left = Expression.Constant("1", type);
right = Expression.Constant("2", type);
// fails at run-time on the next statement
left = Expression.Call(
typeof(string),
"Compare",
new Type[] { type, type },
new Expression[] { left, right });
right = Expression.Constant(0, typeof(int));
I will use the resulting left & right in a Expression.Equal, LessThan, LessThanOrEqual, GreaterThan or GreaterThanOrEqual. That is the reason for the Compare method.
I’m sure its something simple, and I’ve boiled down my code to this simple test case. Anyone see where I’ve gone wrong?
This is the problem in your
Expression.Callcode:That’s trying to call
string.Compare<string, string>– they’re the generic arguments, not the types of the normal parameters. Given that it’s a non-generic method, just use null here.Short but complete program: