Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active November 16, 2017 11:52
Show Gist options
  • Save sebnilsson/5221347 to your computer and use it in GitHub Desktop.
Save sebnilsson/5221347 to your computer and use it in GitHub Desktop.
Convert anonymous type to dynamic
public static ExpandoObject ToExpandoObject(this object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj.GetType()))
expando.Add(property.Name, property.GetValue(obj));
return (ExpandoObject) expando;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment