将字典设置为IList时,VB.Net不会引发编译器错误
我正在更新一个遗留应用程序,它正在从另一个项目中读取一个dll以获取项的字典(Guid、字符串)并使用它们 需求已经改变,返回字典的方法现在返回IList 这是这种奇怪的行为;intellisense没有抛出强制转换错误,编译器也没有。它在运行时尝试将字典设置为IList时才会抛出错误 例如:将字典设置为IList时,VB.Net不会引发编译器错误,.net,vb.net,compiler-errors,.net,Vb.net,Compiler Errors,我正在更新一个遗留应用程序,它正在从另一个项目中读取一个dll以获取项的字典(Guid、字符串)并使用它们 需求已经改变,返回字典的方法现在返回IList 这是这种奇怪的行为;intellisense没有抛出强制转换错误,编译器也没有。它在运行时尝试将字典设置为IList时才会抛出错误 例如: Dim someDictionary As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
Dim someDictionary As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
Dim someList As IList(Of Integer)
someDictionary = someList
你知道为什么编译器没有捕捉到这个吗?当“”打开时,它会给出一个错误:
错误:
错误BC30512:Option Strict On不允许从“System.Collections.Generic.IList(整型)”到“System.Collections.Generic.Dictionary(整型,字符串型)”的隐式转换
我建议您将项目更改为启用Option Strict,以帮助捕获此类内容:)因为VB.Net不检查类型安全性-我从未认为VB是强类型语言。VB允许大量隐式强制转换,而C#在您尝试编译之前会大喊大叫。@IAbstract:Option Explicit和Option Strict为on时,VB的类型非常强…@Jon:我想我是Option错了;)
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
someDictionary = someList