{"id":6250,"date":"2014-04-14T03:22:55","date_gmt":"2014-04-14T03:22:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/can-i-serialize-an-expandoobject-in-net-4-collection-of-common-programming-errors\/"},"modified":"2014-04-14T03:22:55","modified_gmt":"2014-04-14T03:22:55","slug":"can-i-serialize-an-expandoobject-in-net-4-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/14\/can-i-serialize-an-expandoobject-in-net-4-collection-of-common-programming-errors\/","title":{"rendered":"Can I serialize an ExpandoObject in .NET 4?-Collection of common programming errors"},"content":{"rendered":"<p>I can&#8217;t serialize ExpandoObject, but I can manually serialize DynamicObject. So using the TryGetMember\/TrySetMember methods of DynamicObject and implementing ISerializable, I can solve my problem which was really to serialize a dynamic object.<\/p>\n<p>I&#8217;ve implemented the following in my simple test app:<\/p>\n<pre><code>using System;\nusing System.Windows.Forms;\nusing System.IO;\nusing System.Runtime.Serialization;\nusing System.Runtime.Serialization.Formatters.Binary;\nusing System.Collections.Generic;\nusing System.Dynamic;\nusing System.Security.Permissions;\n\nnamespace DynamicTest\n{\n    public partial class Form1 : Form\n    {\n        public Form1()\n        {\n            InitializeComponent();\n        }\n\n        private void button1_Click(object sender, EventArgs e)\n        {            \n            dynamic dynamicContext = new DynamicContext();\n            dynamicContext.Greeting = \"Hello\";\n            this.Text = dynamicContext.Greeting;\n\n            IFormatter formatter = new BinaryFormatter();\n            Stream stream = new FileStream(\"MyFile.bin\", FileMode.Create, FileAccess.Write, FileShare.None);\n            formatter.Serialize(stream, dynamicContext);\n            stream.Close();\n        }\n    }\n\n    [Serializable]\n    public class DynamicContext : DynamicObject, ISerializable\n    {\n        private Dictionary dynamicContext = new Dictionary();\n\n        public override bool TryGetMember(GetMemberBinder binder, out object result)\n        {\n            return (dynamicContext.TryGetValue(binder.Name, out result));\n        }\n\n        public override bool TrySetMember(SetMemberBinder binder, object value)\n        {\n            dynamicContext.Add(binder.Name, value);\n            return true;\n        }\n\n        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]\n        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)\n        {\n            foreach (KeyValuePair kvp in dynamicContext)\n            {\n                info.AddValue(kvp.Key, kvp.Value);\n            }\n        }\n\n        public DynamicContext()\n        {\n        }\n\n        protected DynamicContext(SerializationInfo info, StreamingContext context)\n        {\n            \/\/ TODO: validate inputs before deserializing. See http:\/\/msdn.microsoft.com\/en-us\/library\/ty01x675(VS.80).aspx\n            foreach (SerializationEntry entry in info)\n            {\n                dynamicContext.Add(entry.Name, entry.Value);\n            }\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>and Why does SerializationInfo not have TryGetValue methods? had the missing puzzle piece to keep it simple.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I can&#8217;t serialize ExpandoObject, but I can manually serialize DynamicObject. So using the TryGetMember\/TrySetMember methods of DynamicObject and implementing ISerializable, I can solve my problem which was really to serialize a dynamic object. I&#8217;ve implemented the following in my simple test app: using System; using System.Windows.Forms; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Collections.Generic; using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6250","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6250"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6250\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}