我正在更新旧版应用程序,它正在从另一个项目中读取dll以获取项的字典(Guid,字符串)并使用它们。
要求已更改,并且正在返回Dictionary的方法现在正在返回IList。
这是奇怪的行为;智能感知不会引发转换错误,编译器也不会。尝试将Dictionary设置为IList之前,它不会在运行时引发错误。
例:
Dim someDictionary As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
Dim someList As IList(Of Integer)
someDictionary = someList
关于为什么编译器没有捕捉到这个的任何想法?
最佳答案
当“Option Strict”为On时,它给出一个错误:
Option Strict On
Imports System.Collections.Generic
Public Class Test
Public Shared Sub Main()
Dim someDictionary As Dictionary(Of Integer, String) = _
New Dictionary(Of Integer, String)
Dim someList As IList(Of Integer) = Nothing
someDictionary = someList
End Sub
End Class
错误:error BC30512: Option Strict On disallows implicit conversions from 'System.Collections.Generic.IList(Of Integer)' to 'System.Collections.Generic.Dictionary(Of Integer, String)'.
someDictionary = someList
我建议您更改项目以启用Option Strict,以帮助捕获此类问题:)
关于.net - 将Dictionary设置为IList时VB.Net不会引发编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6602310/