Advertisement
Willcode4cash

Parse<T>

Oct 31st, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.51 KB | None | 0 0
  1. public static T Parse<T>(this string value) {        
  2.     // Get default value for type so if string
  3.     // is empty then we can return default value.
  4.     T result = default(T);
  5.     if (!string.IsNullOrEmpty(value)) {            
  6.         // we are not going to handle exception here
  7.         // if you need SafeParse then you should create
  8.         // another method specially for that.
  9.         TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));
  10.         result = (T)tc.ConvertFrom(value);
  11.         }
  12.     return result;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement