【问题标题】:How can I use delegates to process text?如何使用委托来处理文本?
【发布时间】:2013-03-19 23:59:37
【问题描述】:

我目前正在将 XML 反序列化为函数 processText() 中的对象“X”。我想将一个函数作为参数传递,这样我就可以调用 processText 并将任意规则应用于对象 X。这似乎是使用委托的情况,但我不知道如何利用在线示例...

展示我尝试过的示例:

AiringProcessing ap = new AiringProcessing(localFiles[1]);
//  getZeroLengthAirings is the particular process I want to run during my text processing
AiringDelegate del = new AiringDelegate(ap.getZeroLengthAirings);
ap.processBatch(del);

【问题讨论】:

  • 你为什么不展示你到目前为止尝试过的东西..
  • 添加示例。我可能在这里使用了糟糕的设计,但是我在 AiringProcessing 类中实现了几个函数,我想在文本处理期间将它们用作操作,并且我希望能够选择它们的任何子集传递给processBatch 函数。

标签: c# delegates parameter-passing


【解决方案1】:

要将委托作为参数传递,您需要使用 Action<T>()Func<T>,具体取决于返回值(Action 返回 void)。

这里是一个使用动作的例子:

public void TakeADelegate(Action<string> action, string str)
{
  action(str);
}

通过委托调用它:

this.TakeADelegate((string s) => { ... do work here ...})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 2011-01-29
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多