我想在另一個Action中設置一個Action,但是我傳遞給它的Action最後仍然爲null。這裏有一個簡單化例如:傳遞一個行爲作爲參考
Action<Action<VerifylocalResult>> docollectprint = (vl) =>
{
vl = (vlocres) =>
{
//DoStuff
};
//This returns false, indicating the action has been set:
Console.WriteLine((vl == null).ToString());
};
//Hookups
docollectprint(vlocobj.Action_NotExists);
//This returns true, so the action has not been set:
Console.WriteLine((vlocobj.Action_NotExists==null).ToString());
我已經嘗試過通過一個getter方法,而不是真正的交易,但結果是一樣的..它仍然是空進行到底。
Action<Func<Action<VerifylocalResult>>> docollectprint = (vl) =>
{
Action<VerifylocalResult> theaction = vl();
theaction = (vlocres) =>
{
//DoStuff
};
//This returns false, indicating the action has been set
Console.WriteLine((theaction == null).ToString());
};
//Hookups
docollectprint(() => { return vlocobj.Action_NotExists; });
//This returns true, so the action has not been set
Console.WriteLine((vlocobj.Action_NotExists==null).ToString());
有沒有辦法做到這一點?另外,對不起,如果這之前已經問過,但是當我搜索我發現的是人們試圖做Action<ref string x>
或類似的東西。
更新(解決方案):
Func<string, Action<VerifylocalResult>> docollectprint = (msg) =>
{
Action<VerifylocalResult> vl = (vlocres) =>
{
/*DoStuff*/
};
return vl;
};
//Hookups
vlocobj.Action_NotExists = docollectprint("x");
我明白了,謝謝!我將Action更改爲Func並讓它返回Action。這應該工作..儘管......太快,但不能接受你的答案^^並感謝您的額外信息。 –
natli