![](/img/trans.png)
[英]How to make method in view execute after method in view model is execute?
[英]How to make a method that can execute any other method passed to it
我正在嘗試制作一個將對目錄中的每個文件執行某些操作的應用程序。
那東西應該是某種方法。 但是由於我不知道確切地使用哪種方法,因此我正在嘗試使它成為可能,因此我的“迭代”方法接受任何方法或某種方法引用的參數,因此他可以在每個文件上調用它。
關鍵是要處理每個文件有很多選項,用戶可以選擇所需的文件。 這些選項還必須開放以進行擴展,因此從現在起的一周內,我可能會決定添加一個新選項,這就是為什么我需要這樣做:
一種可以在不事先知道其簽名的情況下調用任何方法的方法。
動作和功能對我不起作用,因為它們需要具體的簽名。 據我所知,(我認為)委托也不能作為方法參數傳遞。
我要實現的示例:
void Iterate(DirectoryInfo dir, method dostuff)
{
foreach(var file in dir.GetFiles())
{
dostuff(file);
//And this is the point where I feel stupid...
//Now I realise I need to pass the methods as Action parameters,
//because the foreach can't know what to pass for the called method
//arguments. I guess this is what Daisy Shipton was trying to tell me.
}
}
您的想法可以實現,但是執行某項功能的功能必須始終具有相同的簽名。 為此,您可以使用預定義的委托類型。 考慮以下代碼段。
public void SomethingExecuter(IEnumerable<string> FileNames, Action<string> Something)
{
foreach (string FileName in FileNames)
{
Something(FileName);
}
}
public void SomethingOne(string FileName)
{
// todo - copy the file with name FileName to some web server
}
public void SomethingTwo(string FileName)
{
// todo - delete the file with name FileName
}
第一功能可以如下使用。
SomethingExecuter(FileNames, SomethingOne);
SomethingExecuter(FileNames, SomethingTwo);
我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.