Skip to content

Instantly share code, notes, and snippets.

@stand-sure
Created February 21, 2023 21:42
Show Gist options
  • Save stand-sure/082f07c157562c3499db56d696793d4f to your computer and use it in GitHub Desktop.
Save stand-sure/082f07c157562c3499db56d696793d4f to your computer and use it in GitHub Desktop.
C# Anonymous Type To Expando
public class ObjectExtensions
{
public static IDictionary<string, object> AnonymousTypeToExpando( this object obj )
{
IDictionary<string, object> retVal = new ExpandoObject( );
if ( obj != null )
{
foreach ( PropertyDescriptor prop in TypeDescriptor.GetProperties( obj.GetType( ) ) )
{
retVal.Add( prop.Name, prop.GetValue( obj ) );
}
}
return retVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment