簡體   English   中英

為什么此功能與此代理具有不兼容的簽名?

[英]Why does this Function have a Non Compatiable Signature with this Delegate?

編譯器告訴我這些簽名是不兼容的,但它們對我來說似乎很好。 我想念什么嗎? [VS 2008]

Public MustOverride Function OverridePickUpLocationFromDeliveryAddress(ByVal objDeliveryAddress As DeliveryLocationWS.DeliveryAddress, _
                                                                       ByRef lstProcessingMessages As List(Of String), _
                                                                       ByVal objProcessorHelperLists As ProcessorHelperLists) As Integer

Public Sub New()    
    Dim fncTest As Func(Of DeliveryLocationWS.DeliveryAddress, List(Of String), ProcessorHelperLists, Integer) = AddressOf OverridePickUpLocationFromDeliveryAddress
End Sub

您的函數通過引用( lstProcessingMessages )獲取參數,這對於您嘗試將其分配給的Func(Of T1, T2, T3, T4)是不可接受的。

據我所知, Func委托不支持“by ref”作為類型參數。

static void Main()
{
    Func<int, int> myDoItFunc1 = DoIt; // Doesn't work because of ref param
    Func<int, int> myDoItFunc2 = DoItFunc; // Does work
}

public static int DoItFunc(int i)
{
    return DoIt(ref i);
}

public static int DoIt(ref int i)
{
    return 0;
}

但是,為什么您lstProcessingMessages通過ref傳遞lstProcessingMessages 這是參考類型。 您是否希望完全替換列表的引用,或者您只能清除並填充?

static void Main()
{
    var myList = new List<int>();

    AddToMyList(myList);

    // myList now contains 3 integers.

    ReplaceList(myList);

    // myList STILL contains 3 integers.

    ReplaceList(ref myList);

    // myList is now an entirely new list.
}

public static void AddToMyList(List<int> myList)
{
    myList.Add(1); // Works because you're calling "add"
    myList.Add(2); // on the *same instance* of my list
    myList.Add(3); // (object is a reference type, and List extends object)
}

public static void ReplaceList(List<int> myList)
{
    myList = new List<int>();
}

public static void ReplaceList(ref List<int> myList)
{
    myList = new List<int>();
}

Func不起作用,因為參數聲明為ByVal 但是,您可以聲明自己的委托類型:

Delegate Function PickUpLocationDelegate(ByVal objDeliveryAddress As DeliveryLocationWS.DeliveryAddress, _
  ByRef lstProcessingMessages As List(Of String), _
  ByVal objProcessorHelperLists As ProcessorHelperLists) As Integer

Public Sub New()
    Dim fncTest As PickUpLocationDelegate = AddressOf OverridePickUpLocationFromDeliveryAddress
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM