Skip to content

Instantly share code, notes, and snippets.

@vishalkdotcom
Created March 13, 2014 09:27
Show Gist options
  • Select an option

  • Save vishalkdotcom/9525059 to your computer and use it in GitHub Desktop.

Select an option

Save vishalkdotcom/9525059 to your computer and use it in GitHub Desktop.
Convert Microsoft.VisualBasic.Collection to Dictionary [C# Extension Method]
public static Dictionary<string, object> ToDictionary(this Collection source)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
if (source != null)
{
BindingFlags flg = BindingFlags.Instance | BindingFlags.NonPublic;
object InternalList = source.GetType().GetMethod("InternalItemsList", flg).Invoke(source, null);
for (int qq = 0; qq <= source.Count - 1; qq++)
{
object item = InternalList.GetType().GetProperty("Item", flg).GetValue(InternalList, new object[] { qq });
string key = item.GetType().GetField("m_Key", flg).GetValue(item).ToString();
object value = item.GetType().GetField("m_Value", flg).GetValue(item);
dic.Add(key, value);
}
}
return dic;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment