[英]Custom logical “and” operator
I have built my own query builder and parser for some use. 我已经建立了自己的查询生成器和解析器以供使用。
it is working fine today. 今天工作正常。
I want to change the api so instead of writing .And()
and .Or()
, I want to write &&
||
我想更改api,而不是编写.And()
和.Or()
,而是要编写&&
||
, like in IQueryable. ,就像在IQueryable中一样。
Is it possible in my situation?: 在我的情况下可能吗?:
interface IQ{
IQ And(IQ q);
IQ Or(IQ q);
}
class StringQ : IQ{
}
class IntQ : IQ{
}
// this is how I write the query today:
IQ q = new StringQ("a == str").Or(new IntQ("b == 1"));
// I want to write the query - not the || statement
IQ q = new StringQ("a == str") || new IntQ("b == 1")
So the ||
因此||
statement will host IQ
in it. 语句将在其中托管IQ
。
something like: 就像是:
interface IQ{
IQ &&(IQ q);
IQ ||(IQ q);
}
I believe it could be done with some effort. 我相信可以通过一些努力来完成。
First, you know that these operators can be overloaded in a way . 首先,您知道这些运算符可以以某种方式重载。
The conditional logical operators cannot be overloaded, but they are evaluated using & and |, which can be overloaded. 条件逻辑运算符不能重载,但是可以使用&和|对其进行求值,可以重载。
Second, you need a little trick to be able to force implementation of overloaded operators - for this, check this SO question. 其次,您需要一些技巧来强制实现重载运算符-为此,请检查此 SO问题。
If you want to know my opinion this effort is not worth the benefits you see in it. 如果您想了解我的观点,那么付出这种努力是不值得的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.