<?xml version="1.0" encoding="iso-8859-9" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwizguide.com/rss_namespace/">
 <channel>
  <title>Datakent Forum : Dynamic in C# 4.0: Introducing the ExpandoObject</title>
  <link>http://forum.datakent.com/</link>
  <description>XML içerik linki; Datakent Forum : C# &amp; ASP.NET : Dynamic in C# 4.0: Introducing the ExpandoObject</description>
  <pubDate>Thu, 30 Apr 2026 03:54:30 +0000</pubDate>
  <lastBuildDate>Fri, 09 Oct 2009 23:18:23 +0000</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.54</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>forum.datakent.com/RSS_post_feed.asp?TID=1795</WebWizForums:feedURL>
  <image>
   <title>Datakent Forum</title>
   <url>http://forum.datakent.com/forum_images/datakent.com_forums.png</url>
   <link>http://forum.datakent.com/</link>
  </image>
  <item>
   <title>Dynamic in C# 4.0: Introducing the ExpandoObject : :) bu c# 4.0 da dinamik nesneler...</title>
   <link>http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3922#3922</link>
   <description>
    <![CDATA[<strong>Yazar:</strong> <a href="http://forum.datakent.com/member_profile.asp?PF=1" rel="nofollow">murat turan</a><br /><strong>Konu:</strong> 1795<br /><strong>Gönderim Zamanı:</strong> 09.Ekim.2009 Saat 23:18<br /><br />:) bu c# 4.0 da dinamik nesneler üzerine bir uygulama. xml den veri okuma ve dinamik nesne ile olayı çok daha basitten halletme. geçen sorduğun soru mu değişiklik? ]]>
   </description>
   <pubDate>Fri, 09 Oct 2009 23:18:23 +0000</pubDate>
   <guid isPermaLink="true">http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3922#3922</guid>
  </item> 
  <item>
   <title>Dynamic in C# 4.0: Introducing the ExpandoObject : murat abi bu pek ayd&#305;nlat&#305;c&#305; olmad&#305;...</title>
   <link>http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3910#3910</link>
   <description>
    <![CDATA[<strong>Yazar:</strong> <a href="http://forum.datakent.com/member_profile.asp?PF=2535" rel="nofollow">bayramoglu_61</a><br /><strong>Konu:</strong> 1795<br /><strong>Gönderim Zamanı:</strong> 05.Ekim.2009 Saat 09:47<br /><br />murat abi bu pek aydınlatıcı olmadı :) mysql üzerindeki değişiklikleri izleyecek bi yapı oluşturmaya çalışıyorum ajax ile.]]>
   </description>
   <pubDate>Mon, 05 Oct 2009 09:47:47 +0000</pubDate>
   <guid isPermaLink="true">http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3910#3910</guid>
  </item> 
  <item>
   <title>Dynamic in C# 4.0: Introducing the ExpandoObject : XElement contactXML =     new...</title>
   <link>http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3908#3908</link>
   <description>
    <![CDATA[<strong>Yazar:</strong> <a href="http://forum.datakent.com/member_profile.asp?PF=1" rel="nofollow">murat turan</a><br /><strong>Konu:</strong> 1795<br /><strong>Gönderim Zamanı:</strong> 05.Ekim.2009 Saat 09:31<br /><br /><PRE =code minmax_bound="true"><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement </SPAN>contactXML =    <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contact"</SPAN>,        <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Name"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"</SPAN>),        <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0144"</SPAN>),        <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Address"</SPAN>,            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Street1"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"123 Main St"</SPAN>),            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"City"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"Mercer Island"</SPAN>),            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"State"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"WA"</SPAN>),            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Postal"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"68042"</SPAN>)        )    );</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">Although LINQ to XML is a good technology and I really love it, those <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN> parts look a little bit annoying. This is how I can rewrite it by using <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT>.</P><PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contact = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>();contact.Name = <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"</SPAN>;contact.Phone = <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0144"</SPAN>;contact.Address = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>();contact.Address.Street = <SPAN style="COLOR: #a31515" minmax_bound="true">"123 Main St"</SPAN>;contact.Address.City = <SPAN style="COLOR: #a31515" minmax_bound="true">"Mercer Island"</SPAN>;contact.Address.State = <SPAN style="COLOR: #a31515" minmax_bound="true">"WA"</SPAN>;contact.Address.Postal = <SPAN style="COLOR: #a31515" minmax_bound="true">"68402"</SPAN>;</PRE><P minmax_bound="true">Just note a couple of things. First, look at the declaration of <FONT face="Courier New" minmax_bound="true">contact</FONT>. <P minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contact = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>(); <P minmax_bound="true">I didn&#8217;t write <SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject </SPAN>contact = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>(), because if I did <FONT face="Courier New" minmax_bound="true">contact</FONT> would be a statically-typed object of the <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> type. And of course, statically-typed variables cannot add members at run time. So I used the new <FONT face="Courier New" minmax_bound="true">dynamic</FONT> keyword instead of a type declaration, and since <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> supports dynamic operations, the code works. <P minmax_bound="true">Second, notice that every time I needed a node to have subnodes, I simply created a new instance of <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> as a member of the <FONT face="Courier New" minmax_bound="true">contact</FONT> object. <P minmax_bound="true">It looks like the <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> example has more code, but it&#8217;s actually easier to read. You can clearly see what subnodes each node contains, and you don&#8217;t need to deal with the parentheses and indentation. But the best part is how you can access the elements now. <P minmax_bound="true">This is the code you need to print the State field in LINQ to XML.<PRE =code minmax_bound="true"><SPAN style="COLOR: #2b91af" minmax_bound="true">Console</SPAN>.WriteLine((<SPAN style="COLOR: blue" minmax_bound="true">string</SPAN>)contactXML.Element(<SPAN style="COLOR: #a31515" minmax_bound="true">"Address"</SPAN>).Element(<SPAN style="COLOR: #a31515" minmax_bound="true">"State"</SPAN>));</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">And this is how it looks with <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT>.</P><PRE =code minmax_bound="true"><SPAN style="COLOR: #2b91af" minmax_bound="true">Console</SPAN>.WriteLine(contact.Address.State);</PRE><P minmax_bound="true"><a href="http://11011.net/software/vspaste" target="_blank"></A>But what if you want to have several Contact nodes? Like in the following LINQ to XML example.</P><PRE =code minmax_bound="true"><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement </SPAN>contactsXML =    <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contacts"</SPAN>,        <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contact"</SPAN>,            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Name"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"</SPAN>),            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0144"</SPAN>)        ),        <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contact"</SPAN>,            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Name"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"Ellen Adams"</SPAN>),            <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>, <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0155"</SPAN>)        )    );</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">Just use a collection of dynamic objects.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contacts = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: blue" minmax_bound="true">dynamic</SPAN>&gt;();contacts.Add(<SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>());contacts&#091;0&#093;.Name = <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"</SPAN>;contacts&#091;0&#093;.Phone = <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0144"</SPAN>;contacts.Add(<SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>());contacts&#091;1&#093;.Name = <SPAN style="COLOR: #a31515" minmax_bound="true">"Ellen Adams"</SPAN>;contacts&#091;1&#093;.Phone = <SPAN style="COLOR: #a31515" minmax_bound="true">"206-555-0155"</SPAN>;</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">Technically speaking, I could write <SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contacts = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>&gt;() and the example would work. However, there are some situations where this could cause problems, because the actual type of the list elements should be <FONT face="Courier New" minmax_bound="true">dynamic</FONT> and not <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT>, and these are two different types. (Once again, references to the <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> objects are statically-typed and do not support dynamic operations.) <P minmax_bound="true">Now, if you want to find all the names in your contact list, just iterate over the collection.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">foreach </SPAN>(<SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>c <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>contacts)    <SPAN style="COLOR: #2b91af" minmax_bound="true">Console</SPAN>.WriteLine(c.Name);</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">Again, this syntax is better than LINQ to XML version.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">foreach </SPAN>(<SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>c <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>contactsXML.Descendants(<SPAN style="COLOR: #a31515" minmax_bound="true">"Name"</SPAN>))    <SPAN style="COLOR: #2b91af" minmax_bound="true">Console</SPAN>.WriteLine((<SPAN style="COLOR: blue" minmax_bound="true">string</SPAN>)c);</PRE><P minmax_bound="true"><a href="http://11011.net/software/vspaste" target="_blank"></A>So far, so good. But one of the main advantages of LINQ to XML is, well, LINQ. How would you query dynamic objects? Although there is still a lot to be done in this particular area, you can query dynamic objects. For example, let&#8217;s find all the phone numbers for the specified name.</P><PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>phones = <SPAN style="COLOR: blue" minmax_bound="true">from </SPAN>c <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>(contacts <SPAN style="COLOR: blue" minmax_bound="true">as </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: blue" minmax_bound="true">dynamic</SPAN>&gt;)             <SPAN style="COLOR: blue" minmax_bound="true">where </SPAN>c.Name == <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"             </SPAN><SPAN style="COLOR: blue" minmax_bound="true">select </SPAN>c.Phone;</PRE><P minmax_bound="true">True, the cast here doesn&#8217;t look like something strictly necessary. Certainly the compiler could have determined at run-time that <FONT face="Courier New" minmax_bound="true">contacts</FONT> is <SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: blue" minmax_bound="true">dynamic</SPAN>&gt;. But as I said, there is still some work to be done in this area. <P minmax_bound="true">Another thing to note is that this trick works only for the <a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" target="_blank"><U>LINQ to Objects</U></A> provider. To use dynamic objects in LINQ to SQL or other LINQ providers, you need to modify the providers themselves, and that&#8217;s a completely different story. <P minmax_bound="true">However, even with the cast, syntax is still better than in a LINQ to XML query.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>phonesXML = <SPAN style="COLOR: blue" minmax_bound="true">from </SPAN>c <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>contactsXML.Elements(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contact"</SPAN>)                <SPAN style="COLOR: blue" minmax_bound="true">where </SPAN>c.Element(<SPAN style="COLOR: #a31515" minmax_bound="true">"Name"</SPAN>).Value == <SPAN style="COLOR: #a31515" minmax_bound="true">"Patrick Hines"                </SPAN><SPAN style="COLOR: blue" minmax_bound="true">select </SPAN>c.Element(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>).Value;</PRE><P minmax_bound="true"><a href="http://11011.net/software/vspaste" target="_blank"></A>Sure, there are some things that look better in LINQ to XML. For example, if you want to delete a phone number for all the contacts, you can write just one line of code in LINQ to XML.</P><PRE =code minmax_bound="true">contactsXML.Elements(<SPAN style="COLOR: #a31515" minmax_bound="true">"Contact"</SPAN>).Elements(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>).Remove();</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">Since C# doesn&#8217;t have syntax for removing object members, you don&#8217;t have an elegant solution here. But <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> implements <SPAN style="COLOR: #2b91af" minmax_bound="true">IDictionary</SPAN>&lt;<SPAN style="COLOR: #2b91af" minmax_bound="true">String</SPAN>, <SPAN style="COLOR: #2b91af" minmax_bound="true">Object</SPAN>&gt; to maintain its list of members, and you can delete a member by deleting a key-value pair.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">foreach </SPAN>(<SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>person <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>contacts)    ((<SPAN style="COLOR: #2b91af" minmax_bound="true">IDictionary</SPAN>&lt;<SPAN style="COLOR: #2b91af" minmax_bound="true">String</SPAN>, <SPAN style="COLOR: #2b91af" minmax_bound="true">Object</SPAN>&gt;)person).Remove(<SPAN style="COLOR: #a31515" minmax_bound="true">"Phone"</SPAN>);</PRE><P minmax_bound="true">There are other useful methods in LINQ to XML like <FONT face="Courier New" minmax_bound="true">Save()</FONT> and <FONT face="Courier New" minmax_bound="true">Load()</FONT>. For <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> you need to write such methods yourself, but probably only once. Here, casting to the <FONT face="Courier New" minmax_bound="true">IDictionary</FONT> interface can help as well. <P minmax_bound="true">And although I&#8217;ve been comparing LINQ to XML and <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> in this post, these two approaches are not &#8220;rivals&#8221;. You can convert <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> to <FONT face="Courier New" minmax_bound="true">XElement</FONT> and vice versa. For example, this is what the <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> to <FONT face="Courier New" minmax_bound="true">XElement</FONT> conversion might look like.<PRE =code minmax_bound="true"><SPAN style="COLOR: blue" minmax_bound="true">private static </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement </SPAN>expandoToXML(<SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>node, <SPAN style="COLOR: #2b91af" minmax_bound="true">String </SPAN>nodeName){    <SPAN style="COLOR: #2b91af" minmax_bound="true">XElement </SPAN>xmlNode = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(nodeName);    <SPAN style="COLOR: blue" minmax_bound="true">foreach </SPAN>(<SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>property <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>(<SPAN style="COLOR: #2b91af" minmax_bound="true">IDictionary</SPAN>&lt;<SPAN style="COLOR: #2b91af" minmax_bound="true">String</SPAN>, <SPAN style="COLOR: #2b91af" minmax_bound="true">Object</SPAN>&gt;)node)    {        <SPAN style="COLOR: blue" minmax_bound="true">if </SPAN>(property.Value.GetType() == <SPAN style="COLOR: blue" minmax_bound="true">typeof</SPAN>(<SPAN style="COLOR: #2b91af" minmax_bound="true">ExpandoObject</SPAN>))            xmlNode.Add(expandoToXML(property.Value, property.Key));        <SPAN style="COLOR: blue" minmax_bound="true">else            if </SPAN>(property.Value.GetType() == <SPAN style="COLOR: blue" minmax_bound="true">typeof</SPAN>(<SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: blue" minmax_bound="true">dynamic</SPAN>&gt;))                <SPAN style="COLOR: blue" minmax_bound="true">foreach </SPAN>(<SPAN style="COLOR: blue" minmax_bound="true">var </SPAN>element <SPAN style="COLOR: blue" minmax_bound="true">in </SPAN>(<SPAN style="COLOR: #2b91af" minmax_bound="true">List</SPAN>&lt;<SPAN style="COLOR: blue" minmax_bound="true">dynamic</SPAN>&gt;)property.Value)                    xmlNode.Add(expandoToXML(element, property.Key));            <SPAN style="COLOR: blue" minmax_bound="true">else                </SPAN>xmlNode.Add(<SPAN style="COLOR: blue" minmax_bound="true">new </SPAN><SPAN style="COLOR: #2b91af" minmax_bound="true">XElement</SPAN>(property.Key, property.Value));    }    <SPAN style="COLOR: blue" minmax_bound="true">return </SPAN>xmlNode;}</PRE><a href="http://11011.net/software/vspaste" target="_blank"></A><P minmax_bound="true">This little trick might help you access all the LINQ to XML functions when you need them but at the same time use more convenient syntax when creating and modifying XML trees. <P minmax_bound="true">Of course, XML is not the only area where you can use <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT>. If you heavily use reflection or work a lot with script objects, you can simplify your code with <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT>. On the other hand, <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> is not the only useful class that the DLR provides. The <FONT face="Courier New" minmax_bound="true">DynamicObject</FONT> class, for example, enables you to take more control over dynamic operations and define what actually happens when you access a member or invoke a method. But that&#8217;s a topic for another blog post. <P minmax_bound="true">One more thing to note is that libraries that look up members by name might someday adopt the DLR and implement the <FONT face="Courier New" minmax_bound="true">IDynamicMetaObjectProvider</FONT> interface. (This interface actually provides all the &#8220;magic&#8221; &#8211; or dynamic dispatch &#8211; for <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> and the dynamic feature in general.) For example, if LINQ to XML implements this interface, you would be able to write <SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contacts = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN>XmlElement() instead of <SPAN style="COLOR: blue" minmax_bound="true">dynamic </SPAN>contacts = <SPAN style="COLOR: blue" minmax_bound="true">new </SPAN>ExpandoObject() and perform the same operations that I have shown in the examples for the <FONT face="Courier New" minmax_bound="true">ExpandoObject</FONT> type. <P minmax_bound="true">All the examples provided in this blog post work in <a href="http://go.microsoft.com/fwlink/?LinkId=151799" target="_blank"><U>Visual Studio 2010 Beta 1</U></A>. If you have any comments or suggestions, you are welcome to post them here or contact the DLR team at <a href="http://www.codeplex.com/dlr" target="_blank"><U>http://www.codeplex.com/dlr</U></A>. You can also write an e-mail to the DLR team at <a href="mailto:dlr@microsoft.com" target="_blank"><U>dlr@microsoft.com</U></A>.<a href="http://11011.net/software/vspaste" target="_blank"></A><a href="http://11011.net/software/vspaste" target="_blank"></A></P><DIV =BlogPostFooter minmax_bound="true">Published Thursday, October 01, 2009 12:49 AM by <a href="http://blogs.msdn.com/user/Profile.aspx?UserID=221393" target="_blank"><U>Alexandra Rusina</U></A> </DIV>]]>
   </description>
   <pubDate>Mon, 05 Oct 2009 09:31:40 +0000</pubDate>
   <guid isPermaLink="true">http://forum.datakent.com/forum_posts.asp?TID=1795&amp;PID=3908#3908</guid>
  </item> 
 </channel>
</rss>