.net - 将Dictionary设置为IList时VB.Net不会引发编译器错误

标签 .net vb.net compiler-errors

我正在更新旧版应用程序,它正在从另一个项目中读取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/

相关文章:

.net - 如何同时更新两个字典

.net - Right$(string, 3) 的含义

c# - 如何在 .NET 的单个单元测试中修改语言环境小数点分隔符?

compiler-errors - 为 Windows 64 位编译一致

rust - 二进制操作 == 不能应用于类型 syn::Path

c# - 在 GridView 的 EditTemplates 中使用 DropDownList

.net - 强签名和更新引用程序集

ios - 候选人的不匹配类型为 'clearly matching type'

c# - XML 比较,用于检查窗口应用程序是否相同或不同

javascript - 在 asp.net 中自动调整面板中子控件的大小