Created
February 21, 2023 21:42
-
-
Save stand-sure/082f07c157562c3499db56d696793d4f to your computer and use it in GitHub Desktop.
C# Anonymous Type To Expando
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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