C# Imap 客户端 - 表达式树的使用
我目前正在开发自己的 .net imap api,我想在 imap SEARCH 命令中合并表达式树的使用,因此例如:
client.Search(m => m.DateRecieved < DateTime.Now)
应该转换为以下内容
imap1 SEARCH SENTBEFORE 8-Sep-2011
因为我不太熟悉表达式树是否可以将该 func 参数转换为指定的命令?我知道我必须检查“m”的每个属性并检查什么是什么,但我不太确定从哪里开始。
谢谢!
I am currently in the progress of developing my own .net imap api, I want to incorporate the use of expression trees in the imap SEARCH command, so for example:
client.Search(m => m.DateRecieved < DateTime.Now)
should be converted to the following
imap1 SEARCH SENTBEFORE 8-Sep-2011
Since I am not to familiar with expression trees is it possible to convert that func parameter into the specified command? I know I will have to check for each property of 'm' and check what is what but I am not quite sure where to start.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开始将搜索函数定义为(假设 m 是一个名为 Splog 的类):
然后有趣的事情就开始了。
然后你可以做一些事情:
注意
这只是为了让你开始。事情很快就会变得复杂。您必须记住的一些事情是:
表达式中的任何内容都不会被计算。因此,在您的示例中,如果您传递表达式
x <; DateTime.Now
,DateTime.Now 不会计算为日期。您将传递 DateTime 的 Now 属性的 PropertyExpression。您需要满足这一点并进行评估以获得当前日期。该示例假设您有一个简单的二进制比较。它不会满足更复杂的比较,例如
(m.DateReceived < DateTime.Now && m.Client == "Spog" ) || m.Ooofgle> 22932
要处理像这样的更复杂的场景,您应该确保您的方法在树中递归地构建您的表达式。这很有趣,也是一次很好的锻炼。
彻底阅读表达式文档。
To start define your search function as (assume m is a class called Splog):
Then the fun begins.
Then you can do stuff :
Note
This is just to get you started. Things get complicated very quickly. Some things you have to bear in mind are :
Nothing in an expression gets evaluated. So in your example if you pass the expresssion
x < DateTime.Now
, DateTime.Now is not evaluated to a Date. You are passed a PropertyExpression for the Now property of DateTime. You would need to cater for this and evaluate to get get the current date.The example assumes you have a simple binary comparison. It will not cater for a more complex comparison like
(m.DateReceived < DateTime.Now && m.Client == "Spog" ) || m.Ooofgle > 22932
To handle more complex scenarios like this you should make sure your method works down the tree recursively to build up your expression. Its a lot of fun and a good exercise.
Read through the Expression documentation thoroughly.