Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Converts a string with the length of 1 to the supplied enum type
- /// </summary>
- /// <typeparam name="T">The enum type that you want to convert the string to</typeparam>
- /// <param name="string">A string, with the length of 1, that maps to a enum item on the enum type you're supplying</param>
- /// <returns>An enum of the supplied type</returns>
- public static T ToEnum<T>(string @string)
- {
- if (string.IsNullOrEmpty(@string))
- {
- throw new ArgumentException("Argument null or empty");
- }
- if (@string.Length > 1)
- {
- throw new ArgumentException("Argument length greater than one");
- }
- return (T)Enum.ToObject(typeof(T), @string[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement