小编Dre*_*rew的帖子

将 Dictionary(Of String, Object) 转换为 Dictionary(Of String, String)

当我尝试将 a 传递给Dictionary(Of String, Object)需要 a 的函数参数时,Dictionary(Of String, String)出现以下错误:

无法将类型为“System.Collections.Generic.Dictionary”2[System.String,System.Object]”的对象强制转换为“System.Collections.Generic.Dictionary”2[System.String,System.String]”。

Object字典中的所有值都是字符串,但字典被声明为String/Object. 我原以为系统能够转换它,但因为它不是我需要自己做的。

我查看了.ToDictionary()原型方法,但所有示例都显示了一个列表被转换为字典。

我发现这个问题对我想要的东西有一个公认的答案,但它是用 C# 编写的,我无法弄清楚到 VB.Net 的转换。

编辑 1

违规代码。显然归结要不然我也只是简单地声明dict1string/string在我的实际代码。

Dim dict1 As New Dictionary(Of String, Object) From {{"key1","value1"}}

SomeFunctionThatExpectsParamToBeDictOfStringString(dict1)
Run Code Online (Sandbox Code Playgroud)

编辑 2

我试过:

SomeFunctionThatExpectsParamToBeDictOfStringString(dict1.ToDictionary(Function(k) k.Key, Function(v) v.Value.ToString()))
Run Code Online (Sandbox Code Playgroud)

但得到:

System.MissingMemberException:未找到类型“Dictionary(Of String,Object)”上的公共成员“ToDictionary”。

vb.net

2
推荐指数
1
解决办法
1352
查看次数

标签 统计

vb.net ×1