Question

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)

Was it helpful?

Solution

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>()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top