我被這個錯誤困住了,但不知道如何解決它。
我正在嘗試將方法作為引數傳遞給另一個函式作為操作。我有一個定義兩種方法的介面:
//ISegment interface
void SetNodeA(in INode node);
void SetNodeB(in INode node);
接下來我創建一個段,我想將此方法傳遞給另一個函式:
ISegment segment = GetSegment();
Execute(start, segment.SetNodeA);
Execute(end, segment.SetNodeB);
我的執行函式如下所示:
void Execute(in EndPoint point, in Action<INode> fnc)
{
Verify(segment);
Verify(point.Node);
fnc?.Invoke(point.Node); //set the node
}
問題是我收到此錯誤:
論點 2:無法從“方法組”轉換為“在行動”
不知道這里的方法組是什么意思或如何修復它。
uj5u.com熱心網友回復:
問題是您的SetNodeA和SetNodeB方法有一個in引數,并且您試圖通過Action<T>不支持、或引數的呼叫它們。inoutref
如果您需要繼續使用in這些方法,那么您可以通過創建自定義委托型別并使用它來代替Action<T>:
public delegate void ActionWithInParam<T>(in T node);
然后,您的Execute方法將是這樣的:
void Execute(in EndPoint point, ActionWithInParam<INode> fnc)
{
Verify(segment);
Verify(point.Node);
fnc?.Invoke(point.Node); //set the node
}
uj5u.com熱心網友回復:
您需要in從簽名中洗掉引數修飾符SetNodeX或使用自定義委托作為第二個Execute引數:
public interface ISegment
{
void SetNodeA(in INode node);
void SetNodeB(INode node); // in removed
}
public delegate void DelegateWithIn(in INode node);
void Execute(in EndPoint point, DelegateWithIn fnc)
{
}
void Execute1(in EndPoint point, Action<INode> fnc)
{
}
Execute(null, segment.SetNodeA); // uses custom delegate
Execute1(null, segment.SetNodeB); // uses Action
Action正如我在評論和代表中提到的那樣,Func不支持in,out和ref引數修飾符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462719.html
標籤:C#
下一篇:R組合資料框中的行和列
