2013-03-19 103 views
0

我目前正在将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); 
+3

你为什么不显示你已经试过迄今为止.. – 2013-03-20 00:03:17

+0

新增的例子。我可能在这里使用了糟糕的设计,但是我在AiringProcessing类中实现了几个函数,我希望在文本处理期间将它用作操作,并且我希望能够选择它们的任何子集来传递给processBatch函数。 – Bowlah 2013-03-20 16:09:50

回答

1

要通过一个委托,你将要使用取决于返回值Action<T>()Func<T>(动作返回void)的参数。

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

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

与委托调用它:

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

谢谢你。东西一直让我有些沮丧 – Bowlah 2013-03-20 16:04:04