@@ -342,9 +342,18 @@ public override bool Remove(string key)
342
342
public override object ? this [ string key ]
343
343
{
344
344
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
+ }
346
353
}
347
354
355
+ internal void DoSetValue ( string key , object ? value ) => base [ key ] = value ;
356
+
348
357
internal string GetConnectionString ( bool includePassword )
349
358
{
350
359
var connectionString = ConnectionString ;
@@ -438,6 +447,7 @@ public static MySqlConnectionStringOption GetOptionForKey(string key) =>
438
447
public IReadOnlyList < string > Keys => m_keys ;
439
448
440
449
public abstract object ? GetObject ( MySqlConnectionStringBuilder builder ) ;
450
+ public abstract void SetObject ( MySqlConnectionStringBuilder builder , object value ) ;
441
451
442
452
protected MySqlConnectionStringOption ( IReadOnlyList < string > keys )
443
453
{
@@ -674,10 +684,12 @@ public T GetValue(MySqlConnectionStringBuilder builder) =>
674
684
builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
675
685
676
686
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 ) ) ;
678
688
679
689
public override object GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
680
690
691
+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
692
+
681
693
private T ChangeType ( object objectValue )
682
694
{
683
695
if ( typeof ( T ) == typeof ( bool ) && objectValue is string booleanString )
@@ -694,9 +706,9 @@ private T ChangeType(object objectValue)
694
706
{
695
707
return ( T ) Enum . Parse ( typeof ( T ) , enumString , ignoreCase : true ) ;
696
708
}
697
- catch ( Exception ex )
709
+ catch ( Exception ex ) when ( ! ( ex is ArgumentException ) )
698
710
{
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 ) ;
700
712
}
701
713
}
702
714
@@ -728,10 +740,12 @@ public T GetValue(MySqlConnectionStringBuilder builder) =>
728
740
builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
729
741
730
742
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 ) ) ;
732
744
733
745
public override object ? GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
734
746
747
+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
748
+
735
749
private static T ChangeType ( object objectValue ) =>
736
750
( T ) Convert . ChangeType ( objectValue , typeof ( T ) , CultureInfo . InvariantCulture ) ;
737
751
@@ -742,7 +756,7 @@ private static T ChangeType(object objectValue) =>
742
756
internal sealed class MySqlConnectionStringReferenceOption < T > : MySqlConnectionStringOption
743
757
where T : class
744
758
{
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 )
746
760
: base ( keys )
747
761
{
748
762
m_defaultValue = defaultValue ;
@@ -753,10 +767,12 @@ public MySqlConnectionStringReferenceOption(IReadOnlyList<string> keys, T? defau
753
767
builder . TryGetValue ( Key , out var objectValue ) ? ChangeType ( objectValue ) : m_defaultValue ;
754
768
755
769
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 ) ) ;
757
771
758
772
public override object ? GetObject ( MySqlConnectionStringBuilder builder ) => GetValue ( builder ) ;
759
773
774
+ public override void SetObject ( MySqlConnectionStringBuilder builder , object value ) => SetValue ( builder , ChangeType ( value ) ) ;
775
+
760
776
private static T ChangeType ( object objectValue ) =>
761
777
( T ) Convert . ChangeType ( objectValue , typeof ( T ) , CultureInfo . InvariantCulture ) ;
762
778
0 commit comments