C# Imap 客户端 - 表达式树的使用

发布于 2024-12-03 16:10:28 字数 319 浏览 0 评论 0原文

我目前正在开发自己的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯了 2024-12-10 16:10:28

开始将搜索函数定义为(假设 m 是一个名为 Splog 的类):

void Search (Expression<Func<Splog, bool>> searchExpression) 

然后有趣的事情就开始了。

然后你可以做一些事情:

string comparison= "";
string compare1= "";
string compare2 = "";

if ( searchExpression.Body is BinaryExpression ) // Ensure the expression is a comparison..
{
    if ( searchExpression.Body.NodyType == ExpressionType.LessThan )
        comparison = "SENTBEFORE";
    else if ( searchExpression.Body.NodyType == ExpressionType.GreaterThan )
        comparison = "SENTAFTER";
    else  if ( searchExpression.Body.NodyType == ExpressionType.Equal )
        comparison = "EQUALS";

    // Then evaluate the left and right portions.
    if ( ( searchExpression.Body as BinaryExpression ).Left is MemberExpression )
         compare1 = ( ( searchExpression.Body as BinaryExpression ).Left as MemberExpression).Member.Name;

    if ( ( searchExpression.Body as BinaryExpression ).Right is MemberExpression )
         compare2 = ( ( searchExpression.Body as BinaryExpression ).Right as MemberExpression).Member.Name;

}

Console.WriteLine ( compare1 + " " + comparison + " " + compare2 );

注意

这只是为了让你开始。事情很快就会变得复杂。您必须记住的一些事情是:

  1. 表达式中的任何内容都不会被计算。因此,在您的示例中,如果您传递表达式 x <; DateTime.Now,DateTime.Now 不会计算为日期。您将传递 DateTime 的 Now 属性的 PropertyExpression。您需要满足这一点并进行评估以获得当前日期。

  2. 该示例假设您有一个简单的二进制比较。它不会满足更复杂的比较,例如 (m.DateReceived < DateTime.Now && m.Client == "Spog" ) || m.Ooofgle> 22932

要处理像这样的更复杂的场景,您应该确保您的方法在树中递归地构建您的表达式。这很有趣,也是一次很好的锻炼。

彻底阅读表达式文档

To start define your search function as (assume m is a class called Splog):

void Search (Expression<Func<Splog, bool>> searchExpression) 

Then the fun begins.

Then you can do stuff :

string comparison= "";
string compare1= "";
string compare2 = "";

if ( searchExpression.Body is BinaryExpression ) // Ensure the expression is a comparison..
{
    if ( searchExpression.Body.NodyType == ExpressionType.LessThan )
        comparison = "SENTBEFORE";
    else if ( searchExpression.Body.NodyType == ExpressionType.GreaterThan )
        comparison = "SENTAFTER";
    else  if ( searchExpression.Body.NodyType == ExpressionType.Equal )
        comparison = "EQUALS";

    // Then evaluate the left and right portions.
    if ( ( searchExpression.Body as BinaryExpression ).Left is MemberExpression )
         compare1 = ( ( searchExpression.Body as BinaryExpression ).Left as MemberExpression).Member.Name;

    if ( ( searchExpression.Body as BinaryExpression ).Right is MemberExpression )
         compare2 = ( ( searchExpression.Body as BinaryExpression ).Right as MemberExpression).Member.Name;

}

Console.WriteLine ( compare1 + " " + comparison + " " + compare2 );

Note

This is just to get you started. Things get complicated very quickly. Some things you have to bear in mind are :

  1. 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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文