/// <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), rasing 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);
}
}
}