簡體   English   中英

C#到VB.Net:為什么轉換為VB時無法編譯?

[英]C# to VB.Net: Why does this fail to compile when converted to VB?

我有這個C#擴展方法,它將擴展Value類型為IList的任何字典。 當我在VB.Net中編寫等效代碼時,我得到以下編譯錯誤:

“擴展方法'添加'有一些永遠無法滿足的類型約束”。

我覺得這實在令人費解的類型約束可以在C#中得到滿足。

所以我的問題是:為什么這在VB中不起作用? 有沒有辦法讓這些相同的類型約束在VB中工作? 轉換代碼時出錯了嗎? 我希望有人可以對此有所了解,因為我已經在這個問題上摸了一會兒。 :)

(如果您感到好奇,擴展方法旨在簡化在單個密鑰下將多個值添加到字典中(例如一個客戶下的多個訂單)。但這並不重要,我只關心我的令人費解的行為觀察VB)。

這是適用的C#版本:

/// <summary>
/// Adds the specified value to the multi value dictionary.
/// </summary>
/// <param name="key">The key of the element to add.</param>
/// <param name="value">The value of the element to add. The value can be null for reference types.</param>
public static void Add<KeyType, ListType, ValueType>(this Dictionary<KeyType, ListType> thisDictionary, 
                                                     KeyType key, ValueType value)
where ListType : IList<ValueType>, new()
{
    //if the dictionary doesn't contain the key, make a new list under the key
    if (!thisDictionary.ContainsKey(key))
    {
        thisDictionary.Add(key, new ListType());
    }

    //add the value to the list at the key index
    thisDictionary[key].Add(value);
}

這是不能編譯的VB版本:

''' <summary> 
''' Adds the specified value to the multi value dictionary. 
''' </summary> 
''' <param name="key">The key of the element to add.</param> 
''' <param name="value">The value of the element to add. The value can be null for reference types.</param> 
<System.Runtime.CompilerServices.Extension()> _
Public Sub Add(Of KeyType, ListType As {IList(Of ValueType), New}, ValueType) _
              (ByVal thisDictionary As Dictionary(Of KeyType, ListType), ByVal key As KeyType, ByVal value As ValueType)
    'if the dictionary doesn't contain the key, make a new list under the key 
    If Not thisDictionary.ContainsKey(key) Then
        thisDictionary.Add(key, New ListType())
    End If

    'add the value to the list at the key index 
    thisDictionary(key).Add(value)
End Sub

只有在出現<System.Runtime.CompilerServices.Extension()>才會出現此問題。 VB編譯器強制要求約束必須僅使用第一個參數進行驗證。 由於擴展方法的第一個參數( Dictionary(Of KeyType, ListType) )依賴於通過IList(Of TValue)約束的第三個參數( ValueType ),因此無法在VB中編譯。

原因在這里解釋: http//msdn.microsoft.com/en-us/library/bb385206.aspx

在這種情況下,VB編譯器可能有點挑剔,因為它必須支持可選參數。 C#中沒有可選參數(尚未)。

請注意,略有差異:

''' <summary> 
''' Adds the specified value to the multi value dictionary. 
''' </summary> 
''' <param name="key">The key of the element to add.</param> 
''' <param name="value">The value of the element to add. The value can be null for reference types.</param> 
<System.Runtime.CompilerServices.Extension()> _
Public Sub Add(Of KeyType, ListType As {New, IList(Of ValueType)}, ValueType) _
              (ByVal thisDictionary As Dictionary(Of KeyType, IList(Of ValueType)), ByVal key As KeyType, ByVal value As ValueType)
    'if the dictionary doesn't contain the key, make a new list under the key 
    If Not thisDictionary.ContainsKey(key) Then
        thisDictionary.Add(key, New ListType())
    End If

    'add the value to the list at the key index 
    thisDictionary(key).Add(value)
End Sub

您可以讓Dictionary包含ListType列表,但必須將其聲明為(Of ..., IList(Of ...))

我懷疑問題是你使用ValueType作為其中一個類型參數的名稱,這是.NET類庫(System.ValueType)中的實際類型。 我可以想象C#和VB.NET以不同的方式處理它。 嘗試使用不同的名稱,如TValue(和TKey只是為了保持一致)。

暫無
暫無

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

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