<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codelab Blog &#187; Anonymous Types</title>
	<atom:link href="http://blog.codelab.co.nz/tag/anonymous-types/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codelab.co.nz</link>
	<description>Technical Articles and News from Codelab Ltd</description>
	<lastBuildDate>Tue, 17 Jan 2012 13:10:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating a generic list of anonymous types</title>
		<link>http://blog.codelab.co.nz/2008/10/04/creating-a-generic-list-of-anonymous-types/</link>
		<comments>http://blog.codelab.co.nz/2008/10/04/creating-a-generic-list-of-anonymous-types/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 09:16:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Anonymous Types]]></category>
		<category><![CDATA[Generic List]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=35</guid>
		<description><![CDATA[Scenerio I have a simple HTML form on a ASP.NET Web page. This form is to post a number of values including several elements that are compulsory to perform several actions on the server. As we know, when we post values to ASP.NET these are available for Request.Form. All we know is the key and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Scenerio </strong></p>
<p style="margin-bottom: 0cm;">I have a simple HTML form on a ASP.NET Web page.  This form is to post a number of values including several elements that are compulsory to perform several actions on the server.   As we know, when we post values to ASP.NET these are available for Request.Form.   All we know is the key and value in the NameValueCollection object.   I want to be able to check if several values are present and if they are not, return the respective element meta-description  rather than the original name.</p>
<blockquote>
<p style="margin-bottom: 0cm;">For Example If we post a name field called formName, we want to return “You must enter data into the fields: Form Name”.</p>
</blockquote>
<p style="margin-bottom: 0cm;"><strong>Solution</strong></p>
<p style="margin-bottom: 0cm;">I used a generic list of a anonymous type. My anonymous type has a Name and a Value property.</p>
<blockquote>
<p style="margin-bottom: 0cm;"><span style="color: #0000ff;"><span style="font-size: x-small;"><span>var nameValueInstance = new { Name=”formName”,Value=”Form Name”}</span></span></span></p>
</blockquote>
<p style="margin-bottom: 0cm;">I created a list factory that pases in a array of generic type interfaces and returns a generic list of the generic type interface.</p>
<blockquote>
<p style="margin-bottom: 0cm;"><span style="color: #0000ff;"><span style="font-size: x-small;"><span>private</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> </span></span></span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>static</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> </span></span></span><span style="color: #2b91af;"><span style="font-size: x-small;"><span>List</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span>&lt;T&gt; MakeList&lt;T&gt;(</span></span></span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>params</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> T[] items)</span></span></span></p>
<p style="margin-bottom: 0cm;"><span style="color: #000000;"> <span style="font-size: x-small;">{</span></span></p>
<p style="margin-bottom: 0cm;"><span style="color: #000000;"> </span><span style="color: #2b91af;"><span style="font-size: x-small;"><span>List</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span>&lt;T&gt; newList = </span></span></span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>new</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> </span></span></span><span style="color: #2b91af;"><span style="font-size: x-small;"><span>List</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span>&lt;T&gt;(items);</span></span></span></p>
<p style="margin-bottom: 0cm;"><span style="color: #000000;"> </span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>return</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> newList;</span></span></span></p>
<p style="margin-bottom: 0cm;"><span style="color: #000000;"> </span></p>
<p style="margin-bottom: 0cm;"><span style="color: #000000;"> <span style="font-size: x-small;"><span>}</span></span></span></p>
</blockquote>
<p style="margin-bottom: 0cm;"><span>I was then able to do the following to create a generic list of anonymous types:</span></p>
<blockquote>
<p style="margin-bottom: 0cm;"><span style="color: #0000ff;"><span style="font-size: x-small;"><span>var</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> fields = MakeList(</span></span></span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>new</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> { Name = </span></span></span><span style="color: #a31515;"><span style="font-size: x-small;"><span>&#8220;price&#8221;</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span>, Value = </span></span></span><span style="color: #a31515;"><span style="font-size: x-small;"><span>&#8220;Price&#8221;</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> }, </span></span></span><span style="color: #0000ff;"><span style="font-size: x-small;"><span>new</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> { Name = </span></span></span><span style="color: #a31515;"><span style="font-size: x-small;"><span>&#8220;productname&#8221;</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span>, Value = </span></span></span><span style="color: #a31515;"><span style="font-size: x-small;"><span>&#8220;Product Name&#8221;</span></span></span><span style="color: #000000;"><span style="font-size: x-small;"><span> });</span></span></span></p>
</blockquote>
<p style="margin-bottom: 0cm;"><span>From here I was able to interate through the list and validate the Form elements and return the nessessary output.</span></p>
<p style="margin-bottom: 0cm;"><strong><span>References:</span></strong></p>
<p style="margin-bottom: 0cm;"><em><span>http://kirillosenkov.blogspot.com/2008/01/how-to-create-generic-list-of-anonymous.html</span></em></p>
<p style="margin-bottom: 0cm;">
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2008/10/04/creating-a-generic-list-of-anonymous-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

