我在這種情況下,我想有一種方法,可以採取其他方法作爲輸入。使用行動<object>作爲方法輸入
輸入法始終只有一個對象作爲輸入,但對象不同。
這或多或少是什麼,我需要能夠做到:
public static void takeAnything(Action<object> inputFunc)
{
Console.WriteLine(inputFunc.Method.Name);
}
public static void test1(MyOwnObject input){
// Do stuff with input object
}
public static void test2(MyOtherOwnObject input){
// Do stuff with input object
}
public static void startSystem(){
takeAnything(test1);
takeAnything(test2);
}
在這種情況下,假人會寫出來:
test1
test2
我只是cant't得到這個工作,所以任何幫助將非常感激。
編輯
我不知道這是可能的,也許不是,但重要的是,通話只是takeAnything(test1)
,不takeAnything<MyOwnObject>(test1)
或其他任何東西。
你究竟在問什麼?你有沒有奉獻'TakeAnything'成一個通用的方法(即'TakeAnything(Action inputFunc')? –
Chris
我試過了,但我只能讓它工作調用它:takeAnything(測試),如果我刪除部分我得到這個錯誤:方法'takeAnything (System.Action )'的類型參數不能從使用推斷。嘗試明確指定類型參數。 我不知道這是甚至可能的,這是我希望你們能幫我弄清楚:) –
Bjarkes