c# – Linq和Equality Operator:类型“System.Int32”的表达式
发布时间:2020-12-15 17:37:06 所属栏目:百科 来源:网络整理
导读:我试图覆盖C#中的等号(==)运算符来处理将任何类型与自定义类型进行比较(自定义类型实际上是一个包装/框在零). 所以我有这个: internal sealed class Nothing{ public override bool Equals(object obj) { if (obj == null || obj is Nothing) return true;
我试图覆盖C#中的等号(==)运算符来处理将任何类型与自定义类型进行比较(自定义类型实际上是一个包装/框在零).
所以我有这个: 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,IEnumerable`1 arguments) at System.Linq.Expressions.Expression.Call(Expression instance,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)),typeof(Nothing)) ); exp = Expression.Equal( Expression.Constant(10,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,Boolean liftToNull) 但是再次直接将所有内容投射到对象作品中: exp = Expression.Equal( Expression.Constant(10,Expression.Constant(null,BindingFlags.Public | BindingFlags.Static) ); 所以我想我有我的解决方法…投掷所有东西来对象,并使用一个自定义的比较方法.我还是很惊讶,Linq不像C#一样正常地进行转换. 解决方法
null有什么问题?重新删除int vs null,try int?:
exp = Expression.Equal( Expression.Constant(10,typeof(int?)),typeof(int?)) ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |