Guest User

Untitled

a guest
Mar 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. public static T Parse<T>(this string value) //where T : IConvertible
  2. {
  3. var tt = Type.GetTypeCode(typeof(T));
  4.  
  5. var hh = typeof(T).GetMethods();
  6.  
  7. MethodInfo TryParseMethod = typeof(T).GetMethod("TryParse", new Type[] { typeof(string), typeof(T).MakeByRefType() });
  8.  
  9. hh.Count();
  10.  
  11. if (TryParseMethod != null)
  12. {
  13. object[] parameters = new object[] { value, null };
  14. if ((bool)TryParseMethod.Invoke(null, parameters))
  15. return (T)parameters[1];
  16. else
  17. return default(T);
  18. }
  19. else
  20. return default(T);
  21. }
  22.  
  23. /*
  24. Int32 i32 = "123".Parse<Int32>();
  25. Int64 i64 = "123".Parse<Int64>();
  26. long l = "123".Parse<long>();
  27. float f = "123.2".Parse<float>();
  28. float f2 = "123,2".Parse<float>();
  29. Int32? ip32 = "null".Parse<Int32?>();
  30. Int32? ip232 = "null".Parse<Int32>();
  31. string rr = "null".Parse<string>();
  32.  
  33. */
Add Comment
Please, Sign In to add comment