我试图覆盖 C# 中的相等 (==) 运算符来处理任何类型与自定义类型的比较(自定义类型实际上是围绕 null 的包装器/框)。
所以我有这个:
internal sealed class Nothing
{
public override bool Equals(object obj)
{
if (obj == null || obj is Nothing)
return true;
else
return false;
}
public static bool operator ==(object x, Nothing y)
{
if ((x == null || x is Nothing) && (y == null || y is Nothing))
return true;
return false;
}
...
}
现在,如果我拨打如下电话:
Nothing n = new Nothing();
bool equal = (10 == n);
它工作得很好。但是,如果我尝试通过 Linq 表达式树做同样的事情:
exp = Expression.Equal(
Expression.Constant(10),
Expression.Constant(new Nothing(), typeof(Nothing))
);
它抛出异常:
System.ArgumentException : Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean op_Equality(System.Object, PARTSFinder.Rules.Runtime.RulesNothing)'
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodInfo method, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.ValidateCallArgs(Expression instance, MethodInfo method, ReadOnlyCollection`1& arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments)
at System.Linq.Expressions.ExpressionCompiler.GenerateBinaryMethod(ILGenerator gen, BinaryExpression b, StackType ask)
关于为什么基本系统可以将 Int32 转换为 Object,但 Linq 不能,或者我该如何解决这个问题的任何想法?
整件事都盯着看,因为 Linq 一开始也无法将 Int32 与 Object 进行比较:
exp = Expression.Equal(
Expression.Constant(10),
Expression.Constant(null)
);
引发异常,指出“System.Int32”和“System.Object”没有比较运算符。
快速跟进:
以下确实可以正常工作:
exp = Expression.Equal(
Expression.Constant(10, typeof(object)),
Expression.Constant(new Nothing(), typeof(Nothing))
);
exp = Expression.Equal(
Expression.Constant(10, typeof(object)),
Expression.Constant(null)
);
因此,专门将所有内容都转换为对象。那么,Linq 是否只是在内部不处理继承?真烦人……
跟进#2:
我还尝试使用自定义比较方法:
exp = Expression.Equal(
Expression.Constant(10),
Expression.Constant(null),
false,
this.GetType().GetMethod("ValueEquals", BindingFlags.Public | BindingFlags.Static)
);
public static bool ValueEquals(object x, object y)
{
if (x == null && y == null)
return true;
if (x.GetType() != y.GetType())
return false;
return x == y;
}
这也会引发异常:
System.InvalidOperationException : The operands for operator 'Equal' do not match the parameters of method 'ValueEquals'.
at System.Linq.Expressions.Expression.GetMethodBasedBinaryOperator(ExpressionType binaryType, Expression left, Expression right, MethodInfo method, Boolean liftToNull)
但是再次将所有内容直接转换为对象有效:
exp = Expression.Equal(
Expression.Constant(10, typeof(object)),
Expression.Constant(null, typeof(object)),
false,
this.GetType().GetMethod("ValueEquals", BindingFlags.Public | BindingFlags.Static)
);
所以我想我有我的解决方法......将所有内容都转换为对象并使用自定义比较方法。我仍然对 Linq 没有像普通 C# 那样自动进行转换感到惊讶。