2013-03-19 48 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