目前正致力于将一些旧的VB6代码包装并转换为.NET,我需要能够使用由巨大的VB6代码价格返回的scripting.dictionary。
我想将其转换为.NET通用词典(Of TKey,TValue)
答案 0 :(得分:2)
解决方案是为scripting.dictionary编写扩展方法以转换为.net字典(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));
}
然后只需使用
horribleDictionary.toDictionary<int,string>()