<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>jaredpar's WebLog : Anonymous Types</title><link>http://blogs.msdn.com/jaredpar/archive/tags/Anonymous+Types/default.aspx</link><description>Tags: Anonymous Types</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Names of Anonymous Type Members</title><link>http://blogs.msdn.com/jaredpar/archive/2007/11/09/names-of-anonymous-type-members.aspx</link><pubDate>Sat, 10 Nov 2007 03:46:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6034007</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/6034007.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=6034007</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=6034007</wfw:comment><description>&lt;p&gt;Recently I was asked how can you get a list of anonymous type member names given a query of anonymous types.&amp;nbsp; The quick answer is that you can use a quick bit of reflection to get back the names.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;Public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function&lt;/span&gt; GetAnonymousTypeMemberNames(&lt;span style="color: rgb(0,0,255)"&gt;Of&lt;/span&gt; T)(&lt;span style="color: rgb(0,0,255)"&gt;ByVal&lt;/span&gt; anonymousType &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; T) &lt;span style="color: rgb(0,0,255)"&gt;As&lt;/span&gt; List(&lt;span style="color: rgb(0,0,255)"&gt;Of&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String&lt;/span&gt;)
        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; type = &lt;span style="color: rgb(0,0,255)"&gt;GetType&lt;/span&gt;(T)
        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; list = &lt;span style="color: rgb(0,0,255)"&gt;New&lt;/span&gt; List(&lt;span style="color: rgb(0,0,255)"&gt;Of&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;String&lt;/span&gt;)
        &lt;span style="color: rgb(0,0,255)"&gt;For&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Each&lt;/span&gt; cur &lt;span style="color: rgb(0,0,255)"&gt;In&lt;/span&gt; type.GetProperties(Reflection.BindingFlags.Public &lt;span style="color: rgb(0,0,255)"&gt;Or&lt;/span&gt; Reflection.BindingFlags.Instance)
            list.Add(cur.Name)
        &lt;span style="color: rgb(0,0,255)"&gt;Next
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;Return&lt;/span&gt; list
    &lt;span style="color: rgb(0,0,255)"&gt;End&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Function&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The longer question is how can you get back a list of anonymous type member names given a query result?&amp;nbsp; As long as you know the query will be populated you can just use the first member to get your result.&lt;/p&gt;&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; q = &lt;span style="color: rgb(0,0,255)"&gt;From&lt;/span&gt; it &lt;span style="color: rgb(0,0,255)"&gt;In&lt;/span&gt; &lt;span style="color: rgb(163,21,21)"&gt;"astring"&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Select&lt;/span&gt; a = it, b = it &amp;amp; &lt;span style="color: rgb(163,21,21)"&gt;"b"
&lt;/span&gt;        GetAnonymousTypeMemberNames(q.First())&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;However you can't guarantee that a query will always have data in it.&amp;nbsp; Another route is to consider the contract of the From ... Select statement.&amp;nbsp; This will produce a query which implements &lt;a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable(Of T).&lt;/a&gt;&amp;nbsp; For a query T will be the anonymous type that is generated[1].&amp;nbsp; We can query the metadata of the returned type to get the System.Type for the anonymous type regardless of the type actually implementing &lt;a href="http://msdn2.microsoft.com/en-us/library/9eekhta0.aspx"&gt;IEnumerable(Of T).&lt;/a&gt;&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; q = &lt;span style="color: rgb(0,0,255)"&gt;From&lt;/span&gt; it &lt;span style="color: rgb(0,0,255)"&gt;In&lt;/span&gt; &lt;span style="color: rgb(163,21,21)"&gt;"astring"&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;Select&lt;/span&gt; a = it, b = it &amp;amp; &lt;span style="color: rgb(163,21,21)"&gt;"b"
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; enumerableInterface = &lt;span style="color: rgb(0,0,255)"&gt;GetType&lt;/span&gt;(IEnumerable(&lt;span style="color: rgb(0,0,255)"&gt;Of&lt;/span&gt; ))
        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; enumerableType = q.GetType().GetInterface(enumerableInterface.FullName)
        &lt;span style="color: rgb(0,0,255)"&gt;Dim&lt;/span&gt; anonymousTypeType = enumerableType.GetGenericArguments(0)&lt;/pre&gt;
&lt;p&gt;[1] This is assuming that you didn't write a query which returns a field directly and avoids creating the anonymous type.&amp;nbsp; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6034007" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Anonymous+Types/default.aspx">Anonymous Types</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Generics/default.aspx">Generics</category></item><item><title>Casting to an Anonymous Type</title><link>http://blogs.msdn.com/jaredpar/archive/2007/10/01/casting-to-an-anonymous-type.aspx</link><pubDate>Mon, 01 Oct 2007 18:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5224427</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/5224427.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=5224427</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=5224427</wfw:comment><description>&lt;P&gt;This discussion is building upon a previous post on how to acquire an &lt;A href="http://blogs.msdn.com/jaredpar/archive/2007/08/01/coding-quiz-anonymous-type-types.aspx" mce_href="http://blogs.msdn.com/jaredpar/archive/2007/08/01/coding-quiz-anonymous-type-types.aspx"&gt;anonymous type ... type&lt;/A&gt;.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The next question is, how can you cast an arbitrary object into an anonymous type?&amp;nbsp; At a glance this doesn't seem possible as you cannot directly express the type of an anonymous type in code.&amp;nbsp; For instance the following code is not legal.&amp;nbsp; &lt;/P&gt;&lt;PRE class=code&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Dim&lt;/SPAN&gt; o &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Object&lt;/SPAN&gt; = SomeCall() 
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Dim&lt;/SPAN&gt; x = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Directcast&lt;/SPAN&gt;(o, &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;New&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;With&lt;/SPAN&gt; {.a = 5})&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Even though it's not possible to directly express the type in code, we can indirectly express it via type inference.&amp;nbsp; Anonymous Types are strongly typed in the compiler and it is possible to infer these types where inference is available.&amp;nbsp; &lt;/P&gt;&lt;PRE class=code&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Function&lt;/SPAN&gt; CastSpecial(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Of&lt;/SPAN&gt; T)(&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;ByVal&lt;/SPAN&gt; o &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Object&lt;/SPAN&gt;, &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;ByVal&lt;/SPAN&gt; t &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;As&lt;/SPAN&gt; T) &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;As&lt;/SPAN&gt; T 
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;    Return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;DirectCast&lt;/SPAN&gt;(o, T)
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt; End&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Function &lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The above function allows us to perform a DirectCast as long as we have an instance of the type we want to cast to.&amp;nbsp; Now we can call the code and passing in the appropriate anonymous type instance.&lt;/P&gt;&lt;PRE class=code&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Dim&lt;/SPAN&gt; o &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;As&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;Object&lt;/SPAN&gt; = SomeCall()
&lt;SPAN style="COLOR: rgb(0,0,255)"&gt; Dim&lt;/SPAN&gt; x = CastSpecial(o, &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;New&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;With&lt;/SPAN&gt; {.a = 5})&lt;/PRE&gt;
&lt;P&gt;There are a couple of caveats to this approach though.&amp;nbsp; &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The actual anonymous type and the one you are casting to must be exactly the same.&amp;nbsp; No polymorphism can be involved.&lt;/LI&gt;
&lt;LI&gt;The anonymous type must be created in the same assembly where you are performing the cast.&amp;nbsp; Anonymous types are specific to the assembly they are created in so performing the cast accross assemblies will cause a InvalidCastException to occur.&lt;/LI&gt;
&lt;LI&gt;It does needlessly create an object to perform the cast.&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5224427" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Anonymous+Types/default.aspx">Anonymous Types</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Generics/default.aspx">Generics</category></item><item><title>Coding Quiz: Anonymous Type Types</title><link>http://blogs.msdn.com/jaredpar/archive/2007/08/01/coding-quiz-anonymous-type-types.aspx</link><pubDate>Wed, 01 Aug 2007 18:34:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4170691</guid><dc:creator>Jared Parsons</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jaredpar/comments/4170691.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaredpar/commentrss.aspx?PostID=4170691</wfw:commentRss><wfw:comment>http://blogs.msdn.com/jaredpar/rsscomments.aspx?PostID=4170691</wfw:comment><description>&lt;p&gt;Question: How can you create a variable in VB which is typed as an anonymous type but not actually create an instance of that type?&lt;/p&gt; &lt;p&gt;Answer in comments.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Note, doing this is not particularly useful it came about while I was playing around with a feature a few days ago.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4170691" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaredpar/archive/tags/VB/default.aspx">VB</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/DotNet/default.aspx">DotNet</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Orcas/default.aspx">Orcas</category><category domain="http://blogs.msdn.com/jaredpar/archive/tags/Anonymous+Types/default.aspx">Anonymous Types</category></item></channel></rss>