Dynamic features of the C#
Afshar
Posted on October 29, 2020
I am a long term user of C# language. C# have been a static language for most of its life time. Recently, I have used JavaScript a lot. Dynamic nature of the JavaScript became handy for me. It is enjoyable when I am not forced to define an entire DTO class when I want to receive or pass a series of the parameters in the JavaScript. All of all is to define members in a JSON format.
When you get used to it, you like to behave alike in the C#. Fortunately, new C# have some dynamic features. It has keyword dynamic, and it contains ExpandoObject.
In my case, I used dynamic features for easily generating a JSON output. By my default style, I should created a series of DTOs, then populate them in a nested loop from database. However, dynamic feature let me exclude all redundant DTO classes and, simply inject data by extending the ExpandoObject.
At first glance, I had confusion about the order in which items would be added to the ExpandoObject or List<dynamic>. But by exercise and test, I found the correct solution.
Next step, I will try to use dynamic feature to receive binding models from the API. This is another area which generates a lot of less used DTOs. By now, I don't know about any other usage of dynamic in the C# and ASP.NET, but I am open for it.
Posted on October 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.