Advertisement
Guest User

Untitled

a guest
Apr 20th, 2010
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1.         /// <summary>
  2.         /// Converts a string with the length of 1 to the supplied enum type
  3.         /// </summary>
  4.         /// <typeparam name="T">The enum type that you want to convert the string to</typeparam>
  5.         /// <param name="string">A string, with the length of 1, that maps to a enum item on the enum type you're supplying</param>
  6.         /// <returns>An enum of the supplied type</returns>
  7.         public static T ToEnum<T>(string @string)
  8.         {
  9.             if (string.IsNullOrEmpty(@string))
  10.             {
  11.                 throw new ArgumentException("Argument null or empty");
  12.             }
  13.             if (@string.Length > 1)
  14.             {
  15.                 throw new ArgumentException("Argument length greater than one");
  16.             }
  17.             return (T)Enum.ToObject(typeof(T), @string[0]);
  18.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement