/// <summary> /// Will parse s into T, provided T has a Parse(string) or TryParse(string s, out T t) method defined, or is one of the magical /// special cases we've implemented (including ICollection (comma delimited), Nullable and Enums). /// </summary> /// <typeparam name="T">T</typeparam> /// <param name="s">s</param> /// <param name="t">t</param> /// <returns>bool</returns> public static bool TryParse <T>(string s, out T t) { Type type = typeof(T); if ((String.IsNullOrEmpty(s)) || (s.Equals("null", StringComparison.CurrentCultureIgnoreCase) && type.IsClass)) { t = default(T); // return null return(true); } s = s.Trim(); if (s.Equals("help", StringComparison.CurrentCultureIgnoreCase)) { throw ArgumentCollection.GetHelpOnKnownSubtypes(type); } if (s.Equals("help!", StringComparison.CurrentCultureIgnoreCase)) { throw ArgumentCollection.CreateHelpMessage(type); } if (s is T) { return(StringTryParse(s, out t)); } if (type.IsEnum) { return(EnumTryParse(s, out t)); } if (type.IsGenericType) { if (type.ParseAsCollection()) { return(CollectionsTryParse(s, out t)); } else if (type.Name.StartsWith("Nullable")) { return(NullableTryParse(s, out t)); } } return(GenericTryParse(s, out t)); }
/// <summary> /// Will parse s into T, provided T has a Parse(string) or TryParse(string s, out T t) method defined, or is one of the magical /// special cases we've implemented (including ICollection (comma delimited), Nullable and Enums). /// </summary> /// <typeparam name="T"></typeparam> /// <param name="s"></param> /// <returns></returns> public static bool TryParse <T>(string s, out T t) { s = s.Trim(); Type type = typeof(T); if (s.Equals("help", StringComparison.CurrentCultureIgnoreCase)) { throw ArgumentCollection.GetHelpOnKnownSubtypes(type); } else if (s.Equals("help!", StringComparison.CurrentCultureIgnoreCase)) { throw ArgumentCollection.CreateHelpMessage(type); } else if (s.Equals("null", StringComparison.CurrentCultureIgnoreCase) && type.IsClass) { t = default(T); // return null return(true); } else if (s is T) { return(StringTryParse(s, out t)); } else if (type.IsEnum) { return(EnumTryParse(s, out t)); } else if (type.IsGenericType) { //if (type.FindInterfaces(Module.FilterTypeNameIgnoreCase, "ICollection*").Length > 0) if (type.ParseAsCollection()) { return(CollectionsTryParse(s, out t)); } else if (type.Name.StartsWith("Nullable")) { return(NullableTryParse(s, out t)); } } return(GenericTryParse(s, out t)); }