<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Ease Parsing enums in C# using generics</title>
	<atom:link href="http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/</link>
	<description>mostly tech, mostly rants</description>
	<lastBuildDate>Tue, 07 Feb 2012 18:44:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: ryan</title>
		<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/comment-page-1/#comment-37436</link>
		<dc:creator>ryan</dc:creator>
		<pubDate>Thu, 18 Feb 2010 16:45:12 +0000</pubDate>
		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/?p=3#comment-37436</guid>
		<description>@Rob Volk: I bet wordpress is eating the angle brackets</description>
		<content:encoded><![CDATA[<p>@Rob Volk: I bet wordpress is eating the angle brackets</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Volk</title>
		<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/comment-page-1/#comment-37434</link>
		<dc:creator>Rob Volk</dc:creator>
		<pubDate>Thu, 18 Feb 2010 16:30:56 +0000</pubDate>
		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/?p=3#comment-37434</guid>
		<description>BTW - you forgot the generic declaration on the method signature.  Should be:

public static T ParseToEnum(this string name)</description>
		<content:encoded><![CDATA[<p>BTW &#8211; you forgot the generic declaration on the method signature.  Should be:</p>
<p>public static T ParseToEnum(this string name)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob Volk</title>
		<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/comment-page-1/#comment-37433</link>
		<dc:creator>Rob Volk</dc:creator>
		<pubDate>Thu, 18 Feb 2010 16:20:26 +0000</pubDate>
		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/?p=3#comment-37433</guid>
		<description>The extension method is what I was looking for.  Works great.  I love open source development via blog comments!  thanks guys.</description>
		<content:encoded><![CDATA[<p>The extension method is what I was looking for.  Works great.  I love open source development via blog comments!  thanks guys.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/comment-page-1/#comment-20865</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Thu, 05 Mar 2009 13:42:16 +0000</pubDate>
		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/?p=3#comment-20865</guid>
		<description>Hey just came across this snippet. 

Very handy. I&#039;ve made it a little more robust, and turned it into an extension method:

public static T ParseToEnum(this string name)
{
    if (false == typeof(T).IsEnum)
        throw new NotSupportedException(typeof(T).Name + &quot; must be an Enum&quot;);

    if(false == Enum.IsDefined(typeof(T), name))
        throw new ArgumentException(string.Format(&quot;{0} is not defined in type of enum {1}&quot;, name, typeof(T).Name));

    return (T)Enum.Parse(typeof(T), name);
}

And some unit tests for it:

[Test]
public void ParseToEnum_ValidStringValidEnum_ReturnsSuccess()
{
    var e = &quot;Success&quot;.ParseToEnum();
    Assert.AreEqual(WebExceptionStatus.Success, e);
}

[Test, ExpectedException(&quot;System.NotSupportedException&quot;)]
public void ParseToEnum_TypeIsNotAnEnum_ThrowsException()
{
    var e = &quot;Success&quot;.ParseToEnum();
}

[Test, ExpectedException(&quot;System.ArgumentException&quot;)]
public void ParseToEnum_StringNotDefinedInEnum_ThrowsException()
{
    var e = &quot;ThisDoesNotExist&quot;.ParseToEnum();
}</description>
		<content:encoded><![CDATA[<p>Hey just came across this snippet. </p>
<p>Very handy. I&#8217;ve made it a little more robust, and turned it into an extension method:</p>
<p>public static T ParseToEnum(this string name)<br />
{<br />
    if (false == typeof(T).IsEnum)<br />
        throw new NotSupportedException(typeof(T).Name + &#8221; must be an Enum&#8221;);</p>
<p>    if(false == Enum.IsDefined(typeof(T), name))<br />
        throw new ArgumentException(string.Format(&#8220;{0} is not defined in type of enum {1}&#8221;, name, typeof(T).Name));</p>
<p>    return (T)Enum.Parse(typeof(T), name);<br />
}</p>
<p>And some unit tests for it:</p>
<p>[Test]<br />
public void ParseToEnum_ValidStringValidEnum_ReturnsSuccess()<br />
{<br />
    var e = &#8220;Success&#8221;.ParseToEnum();<br />
    Assert.AreEqual(WebExceptionStatus.Success, e);<br />
}</p>
<p>[Test, ExpectedException("System.NotSupportedException")]<br />
public void ParseToEnum_TypeIsNotAnEnum_ThrowsException()<br />
{<br />
    var e = &#8220;Success&#8221;.ParseToEnum();<br />
}</p>
<p>[Test, ExpectedException("System.ArgumentException")]<br />
public void ParseToEnum_StringNotDefinedInEnum_ThrowsException()<br />
{<br />
    var e = &#8220;ThisDoesNotExist&#8221;.ParseToEnum();<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://ryepup.unwashedmeme.com/blog/2006/11/17/ease-parsing-enums-in-c-using-generics/comment-page-1/#comment-4</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Mon, 19 Feb 2007 06:04:50 +0000</pubDate>
		<guid isPermaLink="false">http://ryepup.unwashedmeme.com/blog/?p=3#comment-4</guid>
		<description>Thanks for idea</description>
		<content:encoded><![CDATA[<p>Thanks for idea</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.607 seconds -->

