Created
July 24, 2017 12:58
-
-
Save monry/2b96baa27a2daf07cdf37afd75532cc6 to your computer and use it in GitHub Desktop.
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
namespace ForwardCompatibility { | |
// ReSharper disable once PartialTypeWithSinglePart | |
/// <summary> | |
/// .NET 3.5 の System.Enum に不足している機能を提供するための前方互換 Enum クラス | |
/// </summary> | |
public partial class Enum { | |
/// <summary> | |
/// Enum をパースする | |
/// </summary> | |
/// <remarks>型制約として enum に限定できないのでソコは注意が必要</remarks> | |
/// <param name="source">パース対象の文字列</param> | |
/// <param name="destination">宛先の enum</param> | |
/// <typeparam name="T">enum 型</typeparam> | |
/// <returns>パース出来たら真</returns> | |
public static bool TryParse<T>(string source, out T destination) { | |
bool result = false; | |
destination = default(T); | |
string[] names = System.Enum.GetNames(typeof(T)); | |
foreach (string name in names) { | |
if (name != source) { | |
continue; | |
} | |
destination = (T)System.Enum.Parse(typeof(T), name); | |
result = true; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment