public static object Parse(Type type, string enumMemberName, bool ignoreCase) { if (type == null) { throw new ArgumentNullException("type"); } if (enumMemberName == null) { throw new ArgumentNullException("enumMemberName"); } if (!type.IsEnum) { throw new ArgumentException(Strings.EnumUtils_GetNamesAndValues_Underlying_Type_Must_Be_Enum, "type"); } try { return(Enum.Parse(type, enumMemberName, ignoreCase)); } catch (Exception exception) { CoreLevelException coreEx = new CoreLevelException(Strings.EnumUtils_Parse_An_Error_Occurred_While_Parsing_Enum_Value, exception); coreEx.Data.Add("VALUE", enumMemberName); coreEx.Data.Add("TYPE", type.ToString()); coreEx.Data.Add("IGNORECASE", ignoreCase.ToString(CultureInfo.InvariantCulture)); throw coreEx; } }
public static IDictionary <string, TUnderlyingType> GetNamesAndValues <TUnderlyingType>(Type enumType) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (!enumType.IsEnum) { throw new ArgumentException(Strings.EnumUtils_GetNamesAndValues_Underlying_Type_Must_Be_Enum, "enumType"); } Type conversionType = typeof(TUnderlyingType); try { Array values = Enum.GetValues(enumType); string[] names = Enum.GetNames(enumType); IDictionary <string, TUnderlyingType> dictionary = new Dictionary <string, TUnderlyingType>(); for (int i = 0; i < values.Length; i++) { dictionary.Add(names[i], (TUnderlyingType)Convert.ChangeType(values.GetValue(i), conversionType, CultureInfo.InvariantCulture)); } return(dictionary); } catch (Exception exception) { CoreLevelException coreEx = new CoreLevelException(Strings.EnumUtils_GetNamesAndValues_An_Error_Occurred_While_Getting_Names_and_Values_of_Enum_Type, exception); coreEx.Data.Add("TYPE", enumType.ToString()); coreEx.Data.Add("UNDERLYINGTYPE", conversionType.ToString()); throw coreEx; } }
public void GetNamesAndValuesAsValuesDate() { CoreLevelException coreLevelException = Assert.Catch <CoreLevelException>(() => EnumUtils.GetNamesAndValues <Enum1, DateTime>()); Assert.AreEqual(typeof(Enum1).ToString(), coreLevelException.Data["TYPE"]); Assert.AreEqual(typeof(DateTime).ToString(), coreLevelException.Data["UNDERLYINGTYPE"]); }
public void ParseThrowExceptionWhenIgnoreCaseFalse() { CoreLevelException coreLevelException = Assert.Catch <CoreLevelException>(() => EnumUtils.Parse <Enum1>("a", false)); Assert.AreEqual(false.ToString(CultureInfo.InvariantCulture), coreLevelException.Data["IGNORECASE"]); Assert.AreEqual(typeof(Enum1).ToString(), coreLevelException.Data["TYPE"]); Assert.AreEqual("a", coreLevelException.Data["VALUE"]); }
public void Find() { CoreLevelException coreLevelException = new CoreLevelException(); UserLevelException userLevelException = new UserLevelException(coreLevelException); CoreLevelException foundCoreLevelException = ExceptionUtils.Find <CoreLevelException>(userLevelException); Assert.AreSame(coreLevelException, foundCoreLevelException); }
/// <summary> /// Downloads the data. /// </summary> /// <param name="uri">The URI.</param> /// <returns>data as byte array.</returns> public static byte[] DownloadData(Uri uri) { try { using (WebClient client = new WebClient()) { return(client.DownloadData(uri)); } } catch (Exception ex) { CoreLevelException downloadDataException = new CoreLevelException("Error occured while downloading data", ex); downloadDataException.Data.Add("URL", uri); throw downloadDataException; } }
public static bool TryParse(Type enumType, string enumMemberName, bool ignoreCase, out object value) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (enumMemberName == null) { throw new ArgumentNullException("enumMemberName"); } if (!enumType.IsEnum) { throw new ArgumentException(Strings.EnumUtils_GetNamesAndValues_Underlying_Type_Must_Be_Enum, "enumType"); } try { string[] names = Enum.GetNames(enumType); for (int i = 0; i < names.Length; i++) { string str = names[i]; bool @equals = ignoreCase ? str.Equals(enumMemberName, StringComparison.OrdinalIgnoreCase) : str.Equals(enumMemberName); if (@equals) { value = Parse(enumType, enumMemberName, ignoreCase); return(true); } } value = default(object); return(false); } catch (Exception exception) { CoreLevelException coreEx = new CoreLevelException(Strings.EnumUtils_Parse_An_Error_Occurred_While_Parsing_Enum_Value, exception); coreEx.Data.Add("VALUE", enumMemberName); coreEx.Data.Add("TYPE", enumType.ToString()); coreEx.Data.Add("IGNORECASE", ignoreCase.ToString(CultureInfo.InvariantCulture)); throw coreEx; } }