/// <summary> /// Convert the provided value to a SqlConnectionColumnEncryptionSetting. /// </summary> /// <param name="keyword"></param> /// <param name="value"></param> /// <returns></returns> internal static SqlConnectionColumnEncryptionSetting ConvertToColumnEncryptionSetting(string keyword, object value) { if (null == value) { return(DbConnectionStringDefaults.ColumnEncryptionSetting); } string sValue = (value as string); SqlConnectionColumnEncryptionSetting result; if (null != sValue) { if (TryConvertToColumnEncryptionSetting(sValue, out result)) { return(result); } // try again after remove leading & trailing whitespaces. sValue = sValue.Trim(); if (TryConvertToColumnEncryptionSetting(sValue, out result)) { return(result); } // string values must be valid throw ADP.InvalidConnectionOptionValue(keyword); } else { // the value is not string, try other options SqlConnectionColumnEncryptionSetting eValue; if (value is SqlConnectionColumnEncryptionSetting) { // quick path for the most common case eValue = (SqlConnectionColumnEncryptionSetting)value; } else if (value.GetType().IsEnum) { // explicitly block scenarios in which user tries to use wrong enum types, like: // builder["SqlConnectionColumnEncryptionSetting"] = EnvironmentVariableTarget.Process; // workaround: explicitly cast non-SqlConnectionColumnEncryptionSetting enums to int throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionColumnEncryptionSetting), null); } else { try { // Enum.ToObject allows only integral and enum values (enums are blocked above), raising ArgumentException for the rest eValue = (SqlConnectionColumnEncryptionSetting)Enum.ToObject(typeof(SqlConnectionColumnEncryptionSetting), value); } catch (ArgumentException e) { // to be consistent with the messages we send in case of wrong type usage, replace // the error with our exception, and keep the original one as inner one for troubleshooting throw ADP.ConvertFailed(value.GetType(), typeof(SqlConnectionColumnEncryptionSetting), e); } } // ensure value is in valid range if (IsValidColumnEncryptionSetting(eValue)) { return(eValue); } else { throw ADP.InvalidEnumerationValue(typeof(SqlConnectionColumnEncryptionSetting), (int)eValue); } } }
/// <summary> /// This method attempts to convert the given value tp ApplicationIntent enum. The algorithm is: /// * if the value is from type string, it will be matched against ApplicationIntent enum names only, using ordinal, case-insensitive comparer /// * if the value is from type ApplicationIntent, it will be used as is /// * if the value is from integral type (SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64), it will be converted to enum /// * if the value is another enum or any other type, it will be blocked with an appropriate ArgumentException /// /// in any case above, if the converted value is out of valid range, the method raises ArgumentOutOfRangeException. /// </summary> /// <returns>application intent value in the valid range</returns> internal static ApplicationIntent ConvertToApplicationIntent(string keyword, object value) { Debug.Assert(null != value, "ConvertToApplicationIntent(null)"); string sValue = (value as string); ApplicationIntent result; if (null != sValue) { // We could use Enum.TryParse<ApplicationIntent> here, but it accepts value combinations like // "ReadOnly, ReadWrite" which are unwelcome here // Also, Enum.TryParse is 100x slower than plain StringComparer.OrdinalIgnoreCase.Equals method. if (TryConvertToApplicationIntent(sValue, out result)) { return(result); } // try again after remove leading & trailing whitespace. sValue = sValue.Trim(); if (TryConvertToApplicationIntent(sValue, out result)) { return(result); } // string values must be valid throw ADP.InvalidConnectionOptionValue(keyword); } else { // the value is not string, try other options ApplicationIntent eValue; if (value is ApplicationIntent) { // quick path for the most common case eValue = (ApplicationIntent)value; } else if (value.GetType().GetTypeInfo().IsEnum) { // explicitly block scenarios in which user tries to use wrong enum types, like: // builder["ApplicationIntent"] = EnvironmentVariableTarget.Process; // workaround: explicitly cast non-ApplicationIntent enums to int throw ADP.ConvertFailed(value.GetType(), typeof(ApplicationIntent), null); } else { try { // Enum.ToObject allows only integral and enum values (enums are blocked above), raising ArgumentException for the rest eValue = (ApplicationIntent)Enum.ToObject(typeof(ApplicationIntent), value); } catch (ArgumentException e) { // to be consistent with the messages we send in case of wrong type usage, replace // the error with our exception, and keep the original one as inner one for troubleshooting throw ADP.ConvertFailed(value.GetType(), typeof(ApplicationIntent), e); } } // ensure value is in valid range if (IsValidApplicationIntentValue(eValue)) { return(eValue); } else { throw ADP.InvalidEnumerationValue(typeof(ApplicationIntent), (int)eValue); } } }