C#=>在这种特殊情况下,带有参数的 Lambda 表达式似乎过于流动/毫无意义

发布于 2024-10-21 21:18:59 字数 1275 浏览 2 评论 0原文

这就是我现在所拥有的:

SetText 是 WPF 中扩展工具包 RichTextbox 的方法

public void SetText(FlowDocument document, string text)
{   
    Action<FlowDocument,string> action = SetUIText;
    Dispatcher.CurrentDispatcher.BeginInvoke(action,DispatcherPriority.Background, document, text);
}

private void SetUIText(FlowDocument doc, string text)
{
    TextRange tr = new TextRange(doc.ContentStart, doc.ContentEnd);
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
    {
        tr.Load(ms, DataFormats.Rtf);
    }
}

我不想创建一个额外的 SetUIText 方法只是为了将其分配给调度程序的委托。

所以我引入了lambda表达式:

public void SetText(FlowDocument document, string text)
{
    Action<FlowDocument, string> action;

    action = ( doc, txt) =>
    {
        TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
        using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
        {
            tr.Load(ms, DataFormats.Rtf);
        }
    };

    Dispatcher.CurrentDispatcher.BeginInvoke(action,DispatcherPriority.Background, document, text);
}

只需检查lambda的doc,txt参数即可。这两个参数均未使用。我使用的是 lambda 表达式内的文档和文本。

我可以使用 doc 或文档以及 txt 或文本。这个 lambda 表达式值得使用吗?我应该坚持前两种方法吗?

This is what I have now:

SetText is a Method of the Extended Toolkit RichTextbox in WPF

public void SetText(FlowDocument document, string text)
{   
    Action<FlowDocument,string> action = SetUIText;
    Dispatcher.CurrentDispatcher.BeginInvoke(action,DispatcherPriority.Background, document, text);
}

private void SetUIText(FlowDocument doc, string text)
{
    TextRange tr = new TextRange(doc.ContentStart, doc.ContentEnd);
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
    {
        tr.Load(ms, DataFormats.Rtf);
    }
}

I do not want to make an extra SetUIText method just to assign it to the delegate for the dispatcher.

So I introduced the lambda expression:

public void SetText(FlowDocument document, string text)
{
    Action<FlowDocument, string> action;

    action = ( doc, txt) =>
    {
        TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
        using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
        {
            tr.Load(ms, DataFormats.Rtf);
        }
    };

    Dispatcher.CurrentDispatcher.BeginInvoke(action,DispatcherPriority.Background, document, text);
}

Just check the doc,txt parameter of the lambda. Both parameters are NOT used. What I use instead is document and text inside the lambda expression.

I could use doc OR document AND txt OR text. Is this lambda expression worth at all to use it? Should I stick with my first 2 methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

(り薆情海 2024-10-28 21:18:59

我同意 CodeInChaos 的评论:为什么你要为采用两个参数的操作而烦恼?我会使用这个:

public void SetText(FlowDocument document, string text)
{
    Action action = () =>
    {
        TextRange tr = new TextRange(document.ContentStart,
                                              document.ContentEnd);
        using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
        {
            tr.Load(ms, DataFormats.Rtf);
        }
    };

    Dispatcher.CurrentDispatcher.BeginInvoke(action,
                                             DispatcherPriority.Background);
}

请注意,您可以使用匿名方法执行相同的操作,这甚至可能看起来更自然,因为您甚至可以避免编写空参数列表:

Action action = delegate
{
    TextRange tr = new TextRange(document.ContentStart,
                                          document.ContentEnd);
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
    {
        tr.Load(ms, DataFormats.Rtf);
    }
};

I agree with CodeInChaos's comment: Why are you bothering with an action taking two parameters? I would use this:

public void SetText(FlowDocument document, string text)
{
    Action action = () =>
    {
        TextRange tr = new TextRange(document.ContentStart,
                                              document.ContentEnd);
        using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
        {
            tr.Load(ms, DataFormats.Rtf);
        }
    };

    Dispatcher.CurrentDispatcher.BeginInvoke(action,
                                             DispatcherPriority.Background);
}

Note that you can do the same with an anonymous method, which may even look more natural, as you can avoid even writing an empty parameter list:

Action action = delegate
{
    TextRange tr = new TextRange(document.ContentStart,
                                          document.ContentEnd);
    using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
    {
        tr.Load(ms, DataFormats.Rtf);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文