Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字典设置为IList时,VB.Net不会引发编译器错误_.net_Vb.net_Compiler Errors - Fatal编程技术网

将字典设置为IList时,VB.Net不会引发编译器错误

将字典设置为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)

我正在更新一个遗留应用程序,它正在从另一个项目中读取一个dll以获取项的字典(Guid、字符串)并使用它们

需求已经改变,返回字典的方法现在返回IList

这是这种奇怪的行为;intellisense没有抛出强制转换错误,编译器也没有。它在运行时尝试将字典设置为IList时才会抛出错误

例如:

        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