<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1497</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1497" rel="self"/>
  <title>mkoby</title>
  <updated>Tue May 12 20:25:00 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code862</id>
    <published>2009-05-12T20:25:00-07:00</published>
    <updated>2009-05-18T17:01:53-07:00</updated>
    <title>[C#] Generic Lists of Different Types</title>
    <content type="html">&lt;p&gt;I have some code for a Twitter library I'm writing and I need to return a list of a specific type depending on the return XML.  I know there has got to be a better way to do it than I currently am.&lt;/p&gt;

&lt;pre&gt;public IList&amp;lt;IStatus&amp;gt; RepsonseHandler(string responseText)
{
	IList&amp;lt;IStatus&amp;gt; Output = null;
	bool multipleStatus = responseText.Contains(&amp;quot;statuses type=\&amp;quot;array\&amp;quot;&amp;quot;);
	XElement xmlElement = XElement.Load(new XmlTextReader(new StringReader(responseText)));

	if(!multipleStatus)
	{
		IStatus status = Status.ParseStatusXML(responseText);
		status.User = User.ParseUserXml(xmlElement.Element(&amp;quot;user&amp;quot;).ToString());
		
		if(status != null)
		{
			Output = new List&amp;lt;IStatus&amp;gt;();
			Output.Add(status);
		}
	}
	else
	{
		Output = new List&amp;lt;IStatus&amp;gt;();
		//Query for count of statuses to make sure we have some
		var query = from c in xmlElement.Descendants(&amp;quot;status&amp;quot;)
					select c;

		if (query.Count() &amp;gt; 0)
		{
			Output = new List&amp;lt;IStatus&amp;gt;();

			foreach (var status in query)
			{
				IStatus currentStatus = Status.ParseStatusXML(status.ToString());
				currentStatus.User = User.ParseUserXml(status.Element(&amp;quot;user&amp;quot;).ToString());
				Output.Add(currentStatus);
			}
		}
	}


	return Output;
}

	//TODO: Figure out a better way to handle this
public IList&amp;lt;IUser&amp;gt; RepsonseHandler(string responseText, string DoesNothing)
{
	IList&amp;lt;IUser&amp;gt; Output = null;
	bool multipleUsers = responseText.Contains(&amp;quot;users type=\&amp;quot;array\&amp;quot;&amp;quot;);
	XElement xmlElement = XElement.Load(new XmlTextReader(new StringReader(responseText)));

	if (!multipleUsers)
	{
		IUser user = User.ParseUserXml(responseText);
		user.Status = Status.ParseStatusXML(xmlElement.Element(&amp;quot;status&amp;quot;).ToString());

		if (user != null)
		{
			Output = new List&amp;lt;IUser&amp;gt;();
			Output.Add(user);
		}
	}
	else
	{
		Output = new List&amp;lt;IUser&amp;gt;();
		//Query for count of statuses to make sure we have some
		var query = from c in xmlElement.Descendants(&amp;quot;user&amp;quot;)
					select c;

		if (query.Count() &amp;gt; 0)
		{
			Output = new List&amp;lt;IUser&amp;gt;();

			foreach (var user in query)
			{
				IUser currentUser = User.ParseUserXml(user.ToString());
				currentUser.Status = Status.ParseStatusXML(user.Element(&amp;quot;status&amp;quot;).ToString());
				Output.Add(currentUser);
			}
		}
	}


	return Output;
}&lt;/pre&gt;</content>
    <author>
      <name>mkoby</name>
      <email>michael.koby@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/862-generic-lists-of-different-types" rel="alternate"/>
  </entry>
</feed>

