@@ -342,9 +342,18 @@ public override bool Remove(string key)
342342 public override object ? this [ string key ]
343343 {
344344 get => MySqlConnectionStringOption . GetOptionForKey ( key ) . GetObject ( this ) ;
345- set => base [ MySqlConnectionStringOption . GetOptionForKey ( key ) . Key ] = value ;
345+ set
346+ {
347+ var option = MySqlConnectionStringOption . GetOptionForKey ( key ) ;
348+ if ( value is null )
349+ base [ option . Key ] = null ;
350+ else
351+ option . SetObject ( this , value ) ;
352+ }
346353 }
347354
355+ internal void DoSetValue ( string key , object ? value ) => base [ key ] = value ;
356+
348357 internal string GetConnectionString ( bool includePassword )
349358 {
350359 var connectionString = ConnectionString ;
@@ -438,6 +447,7 @@ public static MySqlConnectionStringOption GetOptionForKey(string key) =>
438447 public IReadOnlyList < string > Keys => m_keys ;
439448
440449 public abstract object ? GetObject ( MySqlConnectionStringBuilder builder ) ;
450+ public abstract void SetObject ( MySqlConnectionStringBuilder builder , object value ) ;
441451
442452 protected MySqlConnectionStringOption ( IReadOnlyList < string > keys )
443453 {
@@ -674,10 +684,12 @@ public T GetValue(MySqlConnectionStringBuilder builder) =>
674684 builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
675685
676686 public void SetValue ( MySqlConnectionStringBuilder builder , T value ) =>
677- builder [ Key ] = m_coerce is null ? value : m_coerce ( value ) ;
687+ builder . DoSetValue ( Key , m_coerce is null ? value : m_coerce ( value ) ) ;
678688
679689 public override object GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
680690
691+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
692+
681693 private T ChangeType ( object objectValue )
682694 {
683695 if ( typeof ( T ) == typeof ( bool ) && objectValue is string booleanString )
@@ -694,9 +706,9 @@ private T ChangeType(object objectValue)
694706 {
695707 return ( T ) Enum . Parse ( typeof ( T ) , enumString , ignoreCase : true ) ;
696708 }
697- catch ( Exception ex )
709+ catch ( Exception ex ) when ( ! ( ex is ArgumentException ) )
698710 {
699- throw new InvalidOperationException ( "Value '{0}' not supported for option '{1}'." . FormatInvariant ( objectValue , typeof ( T ) . Name ) , ex ) ;
711+ throw new ArgumentException ( "Value '{0}' not supported for option '{1}'." . FormatInvariant ( objectValue , typeof ( T ) . Name ) , ex ) ;
700712 }
701713 }
702714
@@ -728,10 +740,12 @@ public T GetValue(MySqlConnectionStringBuilder builder) =>
728740 builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
729741
730742 public void SetValue ( MySqlConnectionStringBuilder builder , T ? value ) =>
731- builder [ Key ] = m_coerce is null ? value : m_coerce ( value ) ;
743+ builder . DoSetValue ( Key , m_coerce is null ? value : m_coerce ( value ) ) ;
732744
733745 public override object ? GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
734746
747+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
748+
735749 private static T ChangeType ( object objectValue ) =>
736750 ( T ) Convert . ChangeType ( objectValue , typeof ( T ) , CultureInfo . InvariantCulture ) ;
737751
@@ -742,7 +756,7 @@ private static T ChangeType(object objectValue) =>
742756 internal sealed class MySqlConnectionStringReferenceOption < T > : MySqlConnectionStringOption
743757 where T : class
744758 {
745- public MySqlConnectionStringReferenceOption ( IReadOnlyList < string > keys , T ? defaultValue , Func < T ? , T > ? coerce = null )
759+ public MySqlConnectionStringReferenceOption ( IReadOnlyList < string > keys , T ? defaultValue , Func < T ? , T ? > ? coerce = null )
746760 : base ( keys )
747761 {
748762 m_defaultValue = defaultValue ;
@@ -753,10 +767,12 @@ public MySqlConnectionStringReferenceOption(IReadOnlyList<string> keys, T? defau
753767 builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
754768
755769 public void SetValue ( MySqlConnectionStringBuilder builder , T ? value ) =>
756- builder [ Key ] = m_coerce is null ? value : m_coerce ( value ) ;
770+ builder . DoSetValue ( Key , m_coerce is null ? value : m_coerce ( value ) ) ;
757771
758772 public override object ? GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
759773
774+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
775+
760776 private static T ChangeType ( object objectValue ) =>
761777 ( T ) Convert . ChangeType ( objectValue , typeof ( T ) , CultureInfo . InvariantCulture ) ;
762778
0 commit comments