无法使用数组变量初始化字典

时间:2016-07-05 10:19:06

标签: vb.net idictionary

如何在

中转换{{"variable", "default value"},{"integer","123"}}
 Public PredefinedVariables As New ReadOnlyDictionary(Of String, String) ({{"variable", "default value"},{"integer","123"}})

IDictionary(Of String, String)

(也许你应该首先阅读下面ReadOnlyDictionary的实现)

Sub New(array As Array)
    _dictionary = New Dictionary(Of TKey, TValue) From array
End Sub
由于End of statement expected位于From array

对我不起作用。

所以,我认为唯一的方法是将数组转换为IDictionary,将数组重写为KeyValuePair()以初始化_dictionary,或声明实现IDictionary的类并使用该数组初始化它。然而,前两个似乎是不可能的,我没有找到适合第三个条件的类。

类ReadOnlyDictionary(我使用.NET 3.5来保持与Win7的兼容性..NET ReadOnlyDictionary仅在.NET 4.0 +中可用):

Public Class ReadOnlyDictionary(Of TKey, TValue)
    Inherits ReadOnlyCollectionBase
    Implements IDictionary(Of TKey, TValue),
    IEmptyInterface, 'IDictionary, IReadOnlyDictionary(Of TKey, TValue),ISerializable, IDeserializationCallback
    ICollection(Of KeyValuePair(Of TKey, TValue)), IEnumerable(Of KeyValuePair(Of TKey, TValue)), IEnumerable
    Private ReadOnly _dictionary As IDictionary(Of TKey, TValue)

    Public Sub New()
        _dictionary = New Dictionary(Of TKey, TValue)()
    End Sub

    Public Sub New(dictionary As IDictionary(Of TKey, TValue))
        _dictionary = New Dictionary(Of TKey, TValue)(dictionary)
    End Sub

    Public Sub New(dictionary As IDictionary(Of TKey, TValue), comparer As IComparer(Of TKey))
        _dictionary = New Dictionary(Of TKey, TValue)(dictionary, comparer)
    End Sub

#Region "IDictionary<TKey,TValue> Members"

    Private Sub IDictionary_Add(key As TKey, value As TValue) Implements IDictionary(Of TKey, TValue).Add
        Throw ReadOnlyException()
    End Sub

    Public Function ContainsKey(key As TKey) As Boolean Implements IDictionary(Of TKey, TValue).ContainsKey
        Return _dictionary.ContainsKey(key)
    End Function

    Public ReadOnly Property Keys() As ICollection(Of TKey) Implements IDictionary(Of TKey, TValue).Keys
        Get
            Return _dictionary.Keys
        End Get
    End Property

    Private Function IDictionary_Remove(key As TKey) As Boolean Implements IDictionary(Of TKey, TValue).Remove
        Throw ReadOnlyException()
    End Function

    Public Function TryGetValue(key As TKey, ByRef value As TValue) As Boolean Implements IDictionary(Of TKey, TValue).TryGetValue
        Return _dictionary.TryGetValue(key, value)
    End Function

    Public ReadOnly Property Values() As ICollection(Of TValue) Implements IDictionary(Of TKey, TValue).Values
        Get
            Return _dictionary.Values
        End Get
    End Property

    Public ReadOnly Property Item(key As TKey) As TValue
        Get
            Return _dictionary(key)
        End Get
    End Property

    Default Public Property IDictionary_Item(key As TKey) As TValue Implements IDictionary(Of TKey, TValue).Item
        Get
            Return Me(key)
        End Get
        Set
            Throw ReadOnlyException()
        End Set
    End Property

#End Region

#Region "ICollection<KeyValuePair<TKey,TValue>> Members"

    Private Sub ICollection_Add(item As KeyValuePair(Of TKey, TValue)) Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Add
        Throw ReadOnlyException()
    End Sub

    Private Sub ICollection_Clear() Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Clear
        Throw ReadOnlyException()
    End Sub

    Public Function Contains(item As KeyValuePair(Of TKey, TValue)) As Boolean Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Contains
        Return _dictionary.Contains(item)
    End Function

    Public Sub CopyTo(array As KeyValuePair(Of TKey, TValue)(), arrayIndex As Integer) Implements ICollection(Of KeyValuePair(Of TKey, TValue)).CopyTo
        _dictionary.CopyTo(array, arrayIndex)
    End Sub

    Public Overrides ReadOnly Property Count() As Integer Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Count
        Get
            Return _dictionary.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly() As Boolean Implements ICollection(Of KeyValuePair(Of TKey, TValue)).IsReadOnly
        Get
            Return True
        End Get
    End Property

    Private Function ICollection_Remove(item As KeyValuePair(Of TKey, TValue)) As Boolean Implements ICollection(Of KeyValuePair(Of TKey, TValue)).Remove
        Throw ReadOnlyException()
    End Function

#End Region

#Region "IEnumerable<KeyValuePair<TKey,TValue>> Members"

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of TKey, TValue)) Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)).GetEnumerator
        Return _dictionary.GetEnumerator()
    End Function

#End Region

#Region "IEnumerable Members"

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return GetEnumerator()
    End Function

#End Region

    Private Shared Function ReadOnlyException() As Exception
        Return New NotSupportedException("This dictionary is read-only")
    End Function

End Class

Interface IEmptyInterface

End Interface

Uno答案的一些变化:

    Public Sub New(Key As TKey, Value As TValue)
        Me.New
        _dictionary.Add(Key, Value)
    End Sub

    Public Sub New(KeyValuePairs As KeyValuePair(Of TKey, TValue)())
        Me.New
        _dictionary = KeyValuePairs.ToDictionary(Function(x) x.Key, Function(x) x.Value)
    End Sub

1 个答案:

答案 0 :(得分:2)

如果我理解了您的问题,您希望使用以下语法初始化自定义词典:

Public Value As New ReadOnlyDictionary(Of String, String) From {{"variable", "default value"},{"integer","123"}}

为此,如果您使用的是VB 14.0,则可以将Add方法添加到自定义实现中:

Public Sub Add(key As TKey, value As TValue)
     ' Implementation.
End Sub

请注意,框架已经有一个IReadOnlyDictionary和一个ReadOnlyDictionary,因此您可能不需要实现自定义框架。

不幸的是,必须存在Add方法,因此它会减少&#34; ReadOnly&#34;除非你实现一些运行时检查。 From只是语法糖,所以它完全等同于新增字典和添加值。唯一的选择是将值传递给构造函数。