将VB6 Scripting.Dictionary转换为.NET通用词典 [英] Convert VB6 Scripting.Dictionary to .NET Generic Dictionary

查看:83
本文介绍了将VB6 Scripting.Dictionary转换为.NET通用词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在包装一些旧的VB6代码并将其转换为.NET,我需要能够使用scripting.dictionary,由旧价格的VB6代码返回.

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.

我想将其转换为.NET通用词典(TKey,TValue)

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

推荐答案

解决方案是编写用于scripting.dictionary的扩展方法,以将其转换为.net Dictionary(Of TKey,TValue)

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));
}

然后简单地使用

horribleDictionary.toDictionary<int,string>()

这篇关于将VB6 Scripting.Dictionary转换为.NET通用词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆