-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created EnumHelper class to wrap the Enum.TryParse method so an alter…
…nate implementation can be called in .NET 3.5, which doesn't support Enum.TryParse.
- Loading branch information
1 parent
211cc58
commit 0358ebe
Showing
3 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters