Skip to content

Instantly share code, notes, and snippets.

@monry
Created July 24, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monry/2b96baa27a2daf07cdf37afd75532cc6 to your computer and use it in GitHub Desktop.
Save monry/2b96baa27a2daf07cdf37afd75532cc6 to your computer and use it in GitHub Desktop.
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