andrew4582

EnumHelper

Mar 4th, 2011
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Core {
  5.     public static class EnumHelper {
  6.         public static T Parse<T>(string value) {
  7.             return (T)Enum.Parse(typeof(T),value);
  8.         }
  9.         public static T Parse<T>(string value,T defaultValue) {
  10.             return Parse<T>(value,defaultValue,false);
  11.         }
  12.         public static T Parse<T>(string value,T defaultValue,bool ignoreCase) {
  13.             var names = Enum.GetNames(typeof(T));
  14.             if(names.Where(n => n.EqualsIgnoreCase(value)).Count() == 0)
  15.                 return defaultValue;
  16.             return (T)Enum.Parse(typeof(T),value,ignoreCase);
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment