Serializing Anonymous Types in .Net

In C#, you are not able to serialize an object that does not have a parameterless constructor. At least, XmlSerializer is not able to do this. It seems like BinaryFormatter is able, as per this StackOverflow question.

This means you are not able to Xml serialize some object like this, also:


var myObj = new


MyPropertyString = “prop1value”,

MyPropertyInt = 2,

MyPropertyList = new List { “bananas”, “apples”, “pees” }



I needed to be able to serialize this kind of object to database in a human-readable format so, if you don’t need to output in Xml format specifically, you can try this great Json.NET library by James Newton-King, that worked smoothly for me, as easy as:


string jsonString = JsonConvert.SerializeObject(_myObj);


Surely you can do many other things with this library, which you can find out by yourself.