Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static T Parse<T>(this string value) //where T : IConvertible
- {
- var tt = Type.GetTypeCode(typeof(T));
- var hh = typeof(T).GetMethods();
- MethodInfo TryParseMethod = typeof(T).GetMethod("TryParse", new Type[] { typeof(string), typeof(T).MakeByRefType() });
- hh.Count();
- if (TryParseMethod != null)
- {
- object[] parameters = new object[] { value, null };
- if ((bool)TryParseMethod.Invoke(null, parameters))
- return (T)parameters[1];
- else
- return default(T);
- }
- else
- return default(T);
- }
- /*
- Int32 i32 = "123".Parse<Int32>();
- Int64 i64 = "123".Parse<Int64>();
- long l = "123".Parse<long>();
- float f = "123.2".Parse<float>();
- float f2 = "123,2".Parse<float>();
- Int32? ip32 = "null".Parse<Int32?>();
- Int32? ip232 = "null".Parse<Int32>();
- string rr = "null".Parse<string>();
- */
Add Comment
Please, Sign In to add comment