Skip to content

Instantly share code, notes, and snippets.

@RyuaNerin
Created August 11, 2014 01:13
Show Gist options
  • Save RyuaNerin/ba211255d514ea004ff3 to your computer and use it in GitHub Desktop.
Save RyuaNerin/ba211255d514ea004ff3 to your computer and use it in GitHub Desktop.
//
// net { A = "B", C = 1, D = 0.22 }
// => Dictionary<string, object>
//
public static IDictionary<string, object> PropertyToDictionary(object values)
{
try
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (PropertyInfo p in values.GetType().GetProperties())
{
if (!p.CanRead) continue;
dic.Add(Convert.ToString(p.Name), p.GetValue(values, null));
}
return dic;
}
catch
{
throw new FormatException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment