Applying JSON like grammer in C# to dynamically add properties

Edit


When you wrote too much JavaScript, then switch back to write C#, you would love to see the loose type features of JavaScript appear on C#, so that you don't need to create a temporary class just to pass data between methods. I didn't pay much attention to this problem until today. And after some research I found that C# can have the loose-type-ness with the help of the "dynamic" data type and the "ExpandoObject" Class. With them I can write C# code just like JavaScript esp. in handling class/object.

Here is some sample code:

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;

I still yet to research on the performance of those code compare to the type-safe method of creating an extra class just to store some intermediate information. Anyone did some benchmarking?


Further reading : ExpandoObject Class (System.Dynamic)
Applying JSON like grammer in C# to dynamically add properties Applying JSON like grammer in C# to dynamically add properties Reviewed by DF on 12:51:00 AM Rating: 5
©DF. Powered by Blogger.