I’ve spent most if my time programming in strongly types languages. When I see code that is not implicitly typed my immediate reaction is to expect a world of hurt. However, there are many popular languages that are not strongly typed. Even when using a strongly typed language there are times when you need an object to store some data and don’t need the ceremony of creating a structured class.

The ExpandoObject in .NET allows you to create an object and specify its properties at runtime. You can assign values to new properties as needed. You do not need to define the property or type before assigning the value.

I recently had to convert a .NET object to JSON to display information in a chart. Converting from .NET to JSON is an easy task and there are many libraries to help. One of the more difficult parts is to layout the .NET object structure. In my case, the object structure was complex and changes based on the type of chart I am showing.

Using the ExpandoObject I can model my object in .NET, serialize it, then pass it off to the JavaScript.

Example usage

https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=netframework-4.7&WT.mc_id=docs-email