{"id":3034,"date":"2017-08-04T12:19:00","date_gmt":"2017-08-04T06:49:00","guid":{"rendered":"https:\/\/code4developers.com\/?p=3034"},"modified":"2020-05-07T15:00:02","modified_gmt":"2020-05-07T09:30:02","slug":"expandoobject-in-csharp","status":"publish","type":"post","link":"https:\/\/code4developers.com\/expandoobject-in-csharp\/","title":{"rendered":"ExpandoObject in C#"},"content":{"rendered":"<p>In this article, I would like to share extensible dynamic object in C#, we all have idea about dynamic and extensible object in scripting language. C# .net 4.0 introducing dynamic type that provides a host of dynamic features for the static C# language. With the dynamic keyword, one interesting object comes out to be <strong>ExpandoObject.<\/strong><!--more--><\/p>\n<h3 id=\"expandoobject\"><strong>ExpandoObject<\/strong><\/h3>\n<p>C# .NET 4.0 has a cool new ExpandoObject class, in the System.Dynamic namespace. It provides very dynamic object that allows you to add properties and methods on the fly and then access them again.<\/p>\n<p><strong>For example, you can do like <\/strong><\/p>\n<pre class=\"theme:github lang:c# decode:true\">dynamic school = new ExpandoObject();\r\n\r\nschool.Id = 001;\r\n\r\nschool.Name = \"St.Xavier\";\r\n\r\n\/\/Nested dynamic instance\r\nschool.address = new ExpandoObject();\r\n\r\nschool.address.city = \"Gandhinagar\";\r\n\r\nschool.address.state = \"Gujarat\";\r\n\r\n\/\/Bind dynamic Event\r\nschool.HelloWorld = (Func&lt;string, string&gt;)((string name) =&gt;\r\n{\r\n  return \"Hello \" + name;\r\n});\r\n\r\nConsole.WriteLine(school.Name);\r\nConsole.WriteLine(school.HelloWorld(\".net\"));<\/pre>\n<p><strong>Real time use:<\/strong><\/p>\n<p>Before send rest call response if you want to perform any data structure modification like split data in two different formats (no need makes separate class)<\/p>\n<p>Before send rest call response if you want to set hierarchical data structure of your multiple query output. E.g.:<\/p>\n<p>Query-1 output with list of state and Query-2 output with list of cities. If you want set output in state&gt;&gt;city hierarchical structure like as below it just possible without making separate class.<\/p>\n<pre class=\"theme:github lang:default decode:true\">&lt;State&gt;\r\n    &lt;id&gt;1&lt;\/id&gt;\r\n    &lt;name&gt;Gujarat&lt;\/name&gt;\r\n    &lt;city&gt;\r\n        &lt;iscapital&gt;true&lt;\/iscapital&gt;\r\n        &lt;City_Name&gt;Ganhinagar&lt;\/City_Name &gt;\r\n    &lt;\/city&gt;\r\n    &lt;city&gt;\r\n        &lt;iscapital&gt;false&lt;\/iscapital&gt;\r\n        &lt;City_Name&gt;Bhavnagar&lt;\/City_Name &gt;\r\n    &lt;\/city&gt;\r\n&lt;\/state&gt;<\/pre>\n<p>As cool as <strong>ExpandoObject<\/strong> is it has a few limitations too:<\/p>\n<ul>\n<li>It is a sealed type so you can not use it as a base class<\/li>\n<li>It only works off &#8216;properties&#8217; in the internal Dictionary &#8211; you can&#8217;t expose existing type data<\/li>\n<\/ul>\n<p><a href=\"https:\/\/code4developers.com\/categories\/\"><strong>Click Here<\/strong><\/a> to explore all the technology categories on which our authors write an articles<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I would like to share extensible dynamic object in C#, we all have idea about dynamic and extensible object in scripting language. C# .net 4.0 introducing dynamic&hellip;<\/p>\n","protected":false},"author":10,"featured_media":3039,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45],"tags":[48,46,47],"powerkit_post_featured":[],"class_list":{"0":"post-3034","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-csharp","8":"tag-net","9":"tag-c","10":"tag-expandoobject"},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/08\/csharp.png?fit=300%2C300&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8NAi4-MW","jetpack-related-posts":[{"id":2674,"url":"https:\/\/code4developers.com\/pick-your-programming-language\/","url_meta":{"origin":3034,"position":0},"title":"Pick Your Programming Language","author":"Pawan Ingale","date":"June 27, 2017","format":false,"excerpt":"In the IT industry, new technologies are emerging fast. Staying ahead of everyone in the tech market it is very important for every programmer, with changing necessities and new in-demand programming languages for the next years. So here is the list of some technologies which a programmer can choose to\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/code4developers.com\/category\/programming\/"},"img":{"alt_text":"Programming Languages","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/1-3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3106,"url":"https:\/\/code4developers.com\/javascript-template-literals\/","url_meta":{"origin":3034,"position":1},"title":"Javascript: What Are Template Literals?","author":"Arif Khoja","date":"September 30, 2017","format":false,"excerpt":"ES6 introduced two types of literals: Template Literals and Tagged Template Literals to tag Template Literals. In this post, we cover both of them in depth! In ES6, two types of literals were introduced: Template Literals for string concatenation and expression embedding in a string Tagged Template Literals to tag\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/06\/thumb-1-e1498942297714.png?fit=240%2C269&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2662,"url":"https:\/\/code4developers.com\/ecmascript-5-strict-mode-json\/","url_meta":{"origin":3034,"position":2},"title":"ECMAScript5 Strict Mode, JSON, and More&#8230;.","author":"Arif Khoja","date":"June 25, 2017","format":false,"excerpt":"Introduction There are a number of other new features and APIs that need attention, as well. The largest of which are\u00a0Strict Mode\u00a0and native\u00a0JSON\u00a0support. Strict Mode Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a \u201cstrict\u201d operating context. This\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3468,"url":"https:\/\/code4developers.com\/event-delegation-javascript\/","url_meta":{"origin":3034,"position":3},"title":"Event Delegation in JavaScript","author":"Yatendrasinh Joddha","date":"April 19, 2018","format":false,"excerpt":"Event delegation is a pattern of adding an event listener to a parent node instead, of adding an event listener to the individual node. The added event listener will check for the bubbled event to find matching child element. In this article we will talk about how to implement an\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":2462,"url":"https:\/\/code4developers.com\/local-storage-session-storage\/","url_meta":{"origin":3034,"position":4},"title":"Local Storage and Session Storage","author":"Yatendrasinh Joddha","date":"May 22, 2017","format":false,"excerpt":"What is local Storage? local storage is property for accessing Storage object, which is used to store and retrieve data from user\u2019s browser. It is accessible only at client side not at server side like cookie. Data stored in localStorage is accessible thought all pages under same domain, until user\u2026","rel":"","context":"In &quot;JavaScript&quot;","block_context":{"text":"JavaScript","link":"https:\/\/code4developers.com\/category\/javascript\/"},"img":{"alt_text":"javascript","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2017\/10\/javascript.jpg?fit=750%2C422&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3911,"url":"https:\/\/code4developers.com\/dialogflow-introduction-and-basics\/","url_meta":{"origin":3034,"position":5},"title":"DialogFlow &#8211; Introduction and Basics","author":"Arif Khoja","date":"October 7, 2018","format":false,"excerpt":"In this article we will learn about basics that one needs to know about DialogFlow. Introduction DialogFlow previously known as API.AI is a Natural Language Understanding (NLU) engine which provides its user a platform to develop intelligent chat-bots. The main benefit of using dialogflow is it provides an intuitive and\u2026","rel":"","context":"In &quot;DialogFlow&quot;","block_context":{"text":"DialogFlow","link":"https:\/\/code4developers.com\/category\/dialogflow\/"},"img":{"alt_text":"request_response","src":"https:\/\/i0.wp.com\/code4developers.com\/wp-content\/uploads\/2018\/10\/request_response-768x520-300x203.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/comments?post=3034"}],"version-history":[{"count":7,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3034\/revisions"}],"predecessor-version":[{"id":6986,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/posts\/3034\/revisions\/6986"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media\/3039"}],"wp:attachment":[{"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/media?parent=3034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/categories?post=3034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/tags?post=3034"},{"taxonomy":"powerkit_post_featured","embeddable":true,"href":"https:\/\/code4developers.com\/wp-json\/wp\/v2\/powerkit_post_featured?post=3034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}