Skip to content

Instantly share code, notes, and snippets.

@dmannock
Created August 29, 2015 08:23
Show Gist options
  • Select an option

  • Save dmannock/16955da7bcb5013cb3dc to your computer and use it in GitHub Desktop.

Select an option

Save dmannock/16955da7bcb5013cb3dc to your computer and use it in GitHub Desktop.
public static class DynamicHelpers
{
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(dynamic dyn)
{
Dictionary<TKey, TValue> output = new Dictionary<TKey, TValue>();
foreach (var kvp in dyn as IDictionary<TKey, object>)
{
output.Add(
(TKey)Convert.ChangeType(kvp.Key, typeof(TKey)),
(TValue)Convert.ChangeType(kvp.Value, typeof(TValue))
);
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment