Skip to content

Commit

Permalink
Created EnumHelper class to wrap the Enum.TryParse method so an alter…
Browse files Browse the repository at this point in the history
…nate implementation can be called in .NET 3.5, which doesn't support Enum.TryParse.
  • Loading branch information
NightOwl888 committed Sep 21, 2013
1 parent 211cc58 commit 0358ebe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace MvcSiteMapProvider
{
static class EnumHelper
{
public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result) where TEnum : struct
{
#if NET35
try
{
result = (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase);
return true;
}
catch
{
}
result = default(TEnum);
return false;
#else
return Enum.TryParse<TEnum>(value, ignoreCase, out result);
#endif
}

public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct
{
return EnumHelper.TryParse<TEnum>(value, false, out result);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<Compile Include="DI\ConfigurationSettings.cs" />
<Compile Include="DI\ControllerFactoryDecorator.cs" />
<Compile Include="DI\DependencyResolverDecorator.cs" />
<Compile Include="EnumHelper.cs" />
<Compile Include="ISiteMapNodeProvider.cs" />
<Compile Include="ISiteMapSettings.cs" />
<Compile Include="Reflection\AttributeAssemblyProvider.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ protected virtual void ThrowIfHttpMethodInvalid(ISiteMapNode node)
{
HttpVerbs verbs;
if (String.IsNullOrEmpty(node.HttpMethod) ||
(!Enum.TryParse<HttpVerbs>(node.HttpMethod, true, out verbs) &&
(!EnumHelper.TryParse<HttpVerbs>(node.HttpMethod, true, out verbs) &&
!node.HttpMethod.Equals("*") &&
!node.HttpMethod.Equals("Request", StringComparison.InvariantCultureIgnoreCase)))
{
Expand Down

0 comments on commit 0358ebe

Please sign in to comment.