问题
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