From 0358ebe71689f3ec01484de0d932dbb18d87f0b9 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Sun, 22 Sep 2013 01:53:57 +0700 Subject: [PATCH] Created EnumHelper class to wrap the Enum.TryParse method so an alternate implementation can be called in .NET 3.5, which doesn't support Enum.TryParse. --- .../MvcSiteMapProvider/EnumHelper.cs | 31 +++++++++++++++++++ .../MvcSiteMapProvider.csproj | 1 + .../MvcSiteMapProvider/SiteMap.cs | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/MvcSiteMapProvider/MvcSiteMapProvider/EnumHelper.cs diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/EnumHelper.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/EnumHelper.cs new file mode 100644 index 00000000..78494628 --- /dev/null +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/EnumHelper.cs @@ -0,0 +1,31 @@ +using System; + +namespace MvcSiteMapProvider +{ + static class EnumHelper + { + public static bool TryParse(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(value, ignoreCase, out result); +#endif + } + + public static bool TryParse(string value, out TEnum result) where TEnum : struct + { + return EnumHelper.TryParse(value, false, out result); + } + } +} + diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/MvcSiteMapProvider.csproj b/src/MvcSiteMapProvider/MvcSiteMapProvider/MvcSiteMapProvider.csproj index a312be44..ecf17ef8 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/MvcSiteMapProvider.csproj +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/MvcSiteMapProvider.csproj @@ -164,6 +164,7 @@ + diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs index 4771ef4c..45ab4c9b 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMap.cs @@ -809,7 +809,7 @@ protected virtual void ThrowIfHttpMethodInvalid(ISiteMapNode node) { HttpVerbs verbs; if (String.IsNullOrEmpty(node.HttpMethod) || - (!Enum.TryParse(node.HttpMethod, true, out verbs) && + (!EnumHelper.TryParse(node.HttpMethod, true, out verbs) && !node.HttpMethod.Equals("*") && !node.HttpMethod.Equals("Request", StringComparison.InvariantCultureIgnoreCase))) {