Convert VB6 Scripting.Dictionary to .NET Generic Dictionary

喜欢而已 提交于 2020-01-02 13:58:31

问题


Currently im working on wrapping and converting some old VB6 code to .NET and i need to be able to use the scripting.dictionary returned by huge old price of VB6 code.

I want to convert this to the .NET generic Dictionary(Of TKey, TValue)


回答1:


The solution is to write an extension method for scripting.dictionary to convert to a .net Dictionary(Of TKey, 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));
}

and then simply use

horribleDictionary.toDictionary<int,string>()


来源:https://stackoverflow.com/questions/21136733/convert-vb6-scripting-dictionary-to-net-generic-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!