@@ -146,7 +146,7 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
146146 var obj = new Setting ( ) ;
147147 var properties = obj . GetType ( ) . GetProperties ( ) ;
148148
149- var method = this . GetType ( ) ? . GetMethod ( nameof ( Get ) , [ typeof ( string ) ] )
149+ var getMethod = this . GetType ( ) ? . GetMethod ( nameof ( Get ) , [ typeof ( string ) ] )
150150 ?? throw new InvalidOperationException ( $ "{ nameof ( Get ) } does not exist") ;
151151
152152 if ( func is not null )
@@ -158,8 +158,8 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
158158 {
159159 if ( property . CanWrite )
160160 {
161- var genericMethod = method . MakeGenericMethod ( property . PropertyType ) ;
162- var value = genericMethod . Invoke ( this , [ property. Name ] ) ;
161+ var getGenericMethod = getMethod . MakeGenericMethod ( property . PropertyType ) ;
162+ var value = getGenericMethod . Invoke ( this , [ property. Name ] ) ;
163163 property . SetValue ( obj , value ) ;
164164 }
165165 }
@@ -170,29 +170,43 @@ public Setting SaveSettingsToObject(Func<string, bool>? func = null)
170170 public async Task SetSettingsFromObjectAsync ( Setting obj , Func < string , bool > ? func = null )
171171 {
172172 var properties = obj . GetType ( ) . GetProperties ( ) ;
173- var method = this . GetType ( ) ? . GetMethod ( nameof ( SetAsync ) )
173+ var setAsyncMethod = this . GetType ( ) ? . GetMethod ( nameof ( SetAsync ) )
174174 ?? throw new InvalidOperationException ( $ "{ nameof ( SetAsync ) } does not exist") ;
175+ var getMethod = this . GetType ( ) ? . GetMethod ( nameof ( Get ) , [ typeof ( string ) ] )
176+ ?? throw new InvalidOperationException ( $ "{ nameof ( Get ) } does not exist") ;
175177
176178 if ( func is not null )
177179 {
178180 properties = properties . Where ( it => func . Invoke ( it . Name ) ) . ToArray ( ) ;
179181 }
180182
183+ var removeKeys = new List < string > ( ) ;
181184 foreach ( var property in properties )
182185 {
183186 if ( property . CanRead && DefalutSettings . TryGetValue ( property . Name , out var defaultValue ) )
184187 {
185188 var value = property . GetValue ( obj ) ;
186- if ( value == defaultValue )
189+ var getGenericMethod = getMethod . MakeGenericMethod ( property . PropertyType ) ;
190+ var currentValue = getGenericMethod . Invoke ( this , [ property. Name ] ) ;
191+
192+ if ( Object . Equals ( value , currentValue ) )
187193 {
188194 continue ;
189195 }
190196
191- var genericMethod = method . MakeGenericMethod ( property . PropertyType ) ;
192- await ( Task ) genericMethod . Invoke ( this , [ property. Name , value ] ) ! ;
197+ if ( Object . Equals ( value , defaultValue ) )
198+ {
199+ removeKeys . Add ( property . Name ) ;
200+ continue ;
201+ }
202+
203+ var setAsyncGenericMethod = setAsyncMethod . MakeGenericMethod ( property . PropertyType ) ;
204+ await ( Task ) setAsyncGenericMethod . Invoke ( this , [ property. Name , value ] ) ! ;
193205 }
194206 }
195207
208+ await RemoveAsync ( removeKeys ) ;
209+
196210 SettingsChanged ? . Invoke ( ) ;
197211 }
198212 }
0 commit comments