Last active
August 29, 2015 13:57
-
-
Save baba-s/9872541 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| using System; | |
| /// <summary> | |
| /// 列挙型の汎用関数を管理するクラス | |
| /// </summary> | |
| public static partial class EnumCommon | |
| { | |
| /// <summary> | |
| /// 指定された文字列を列挙型に変換します | |
| /// </summary> | |
| /// <typeparam name="T">列挙型</typeparam> | |
| /// <param name="value">変換する文字列</param> | |
| /// <returns>列挙型のオブジェクト</returns> | |
| public static T Parse<T>(string value) | |
| { | |
| return Parse<T>(value, true); | |
| } | |
| /// <summary> | |
| /// 指定された文字列を列挙型に変換します | |
| /// </summary> | |
| /// <typeparam name="T">列挙型</typeparam> | |
| /// <param name="value">変換する文字列</param> | |
| /// <param name="ignoreCase">大文字と小文字を区別しない場合は true</param> | |
| /// <returns>列挙型のオブジェクト</returns> | |
| public static T Parse<T>(string value, bool ignoreCase) | |
| { | |
| return (T)Enum.Parse(typeof(T), value, ignoreCase); | |
| } | |
| /// <summary> | |
| /// 指定された文字列を列挙型に変換して成功したかどうかを返します | |
| /// </summary> | |
| /// <typeparam name="T">列挙型</typeparam> | |
| /// <param name="value">変換する文字列</param> | |
| /// <param name="result">列挙型のオブジェクト</param> | |
| /// <returns>正常に変換された場合は true</returns> | |
| public static bool TryParse<T>(string value, out T result) | |
| { | |
| return TryParse<T>(value, true, out result); | |
| } | |
| /// <summary> | |
| /// 指定された文字列を列挙型に変換して成功したかどうかを返します | |
| /// </summary> | |
| /// <typeparam name="T">列挙型</typeparam> | |
| /// <param name="value">変換する文字列</param> | |
| /// <param name="ignoreCase">大文字と小文字を区別しない場合は true</param> | |
| /// <param name="result">列挙型のオブジェクト</param> | |
| /// <returns>正常に変換された場合は true</returns> | |
| public static bool TryParse<T>(string value, bool ignoreCase, out T result) | |
| { | |
| try | |
| { | |
| result = (T)Enum.Parse(typeof (T), value, ignoreCase); | |
| return true; | |
| } | |
| catch | |
| { | |
| result = default(T); | |
| return false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment