.net - 将 VB6 Scripting.Dictionary 转换为. NET 通用词典

  显示原文与译文双语对照的内容

目前我正在打包和转换一些旧的oauth代码,我需要能够使用. NET 代码返回的scripting.dictionary 。

我想把它转换为. NET 通用字典( Of,TValue )

时间: 作者:

解决方案是为 scripting.dictionary 编写一个扩展方法来转换为. NET 字典( Of,TValue )

VB.net


<Extension()>
Public Function ToDictionary(Of T, T2)(dic As Scripting.Dictionary) As Dictionary(Of T, T2)
 Return dic.Cast(Of Object)().ToDictionary(Function(i) CType(i, T), Function(i) CType(dic.Item(i), T2))
End Function

C#.NET


[Extension()]
public static Dictionary<T, T2> ToDictionary<T, T2>(Scripting.Dictionary dic)
{
 return dic.Cast<object>().ToDictionary(i => (T)i, i => (T2)dic.Item(i));
}

然后用


horribleDictionary.toDictionary<int,string>()

作者:
...