コード例 #1
0
 /// <summary>
 /// Применяет "TryParse" для указанного типа
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="s"></param>
 /// <returns></returns>
 public static Nullable <T> To <T>(this string s) where T : struct
 {
     if (!string.IsNullOrEmpty(s))
     {
         var args = new object[] { s.ReplaceDecimalSeparator(), default(T) };
         if (UtilsReflection.ExecStaticMethod <bool>(typeof(T), "TryParse", args))
         {
             return((T)args[1]);
         }
     }
     return(null);
 }
コード例 #2
0
        /// <summary>
        /// Пытаерся применить typeof(T).TryParse()
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool TryParse <T>(string s, out T obj)
        {
            obj = default(T);
            var res = false;

            if (!string.IsNullOrEmpty(s))             //&& typeof(T).IsValueType)
            {
                var t = typeof(T).FromNullable();

                if (t == typeof(Guid) && !s.StartsWith("{"))
                {
                    s = "{" + s + "}";
                }

                var args = new object[] { s.ReplaceDecimalSeparator(), obj };
                res = UtilsReflection.ExecStaticMethod <bool>(t, "TryParse", args);
                obj = (T)args[1];
            }
            return(res);
        }