@@ -200,7 +200,7 @@ private static PropertyInfo[] GetCachedProperties(Type type)
200200 /// <param name="useEnvironmentVariables">是否使用环境变量</param>
201201 public OptionsBuilder ( string [ ] args , BoolArgumentFormat boolFormat = BoolArgumentFormat . Flag , bool ensurePrefixedKeys = true , bool useEnvironmentVariables = true )
202202 {
203- _args = args ;
203+ _args = args ?? Array . Empty < string > ( ) ;
204204 _boolFormat = boolFormat ;
205205 _useEnvironmentVariables = useEnvironmentVariables ;
206206 _ensurePrefixedKeys = ensurePrefixedKeys ;
@@ -295,7 +295,7 @@ private void ApplyDefaultValues(T target)
295295 var optionAttrs = property . GetCustomAttributes < OptionAttribute > ( ) . ToList ( ) ;
296296 foreach ( var optionAttr in optionAttrs )
297297 {
298- if ( optionAttr != null && optionAttr . DefaultValue != null )
298+ if ( optionAttr . DefaultValue != null )
299299 {
300300 try
301301 {
@@ -304,10 +304,23 @@ private void ApplyDefaultValues(T target)
304304 property . SetValue ( target , convertedValue ) ;
305305 break ; // 只应用第一个找到的默认值
306306 }
307- catch ( Exception ex )
307+ catch ( InvalidCastException ex )
308308 {
309- Console . WriteLine ( $ "设置属性 { property . Name } 的默认值时发生错误 (An error occurred while setting default value for property { property . Name } ): { ex . Message } ") ;
310- Console . WriteLine ( ex ) ;
309+ // 类型转换失败,保持属性的默认状态
310+ // Type conversion failed, keep the property's default state
311+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 的默认值时发生类型转换错误: { ex . Message } ") ;
312+ }
313+ catch ( FormatException ex )
314+ {
315+ // 格式转换失败,保持属性的默认状态
316+ // Format conversion failed, keep the property's default state
317+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 的默认值时发生格式错误: { ex . Message } ") ;
318+ }
319+ catch ( OverflowException ex )
320+ {
321+ // 数值溢出,保持属性的默认状态
322+ // Numeric overflow, keep the property's default state
323+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 的默认值时发生溢出错误: { ex . Message } ") ;
311324 }
312325 }
313326 }
@@ -473,8 +486,9 @@ private Dictionary<string, object> GetEnvironmentVariables()
473486 }
474487 catch ( Exception ex )
475488 {
476- Console . WriteLine ( $ "获取环境变量时发生错误 (An error occurred while retrieving environment variables): { ex . Message } ") ;
477- Console . WriteLine ( ex ) ;
489+ // 忽略环境变量获取错误,返回空字典
490+ // Ignore environment variable retrieval errors, return empty dictionary
491+ System . Diagnostics . Debug . WriteLine ( $ "获取环境变量时发生错误: { ex . Message } ") ;
478492 }
479493
480494 return result ;
@@ -783,9 +797,17 @@ private void ApplyOptions(T target, Dictionary<string, object> options)
783797 {
784798 convertedValue = Convert . ChangeType ( stringValue , property . PropertyType ) ;
785799 }
786- catch
800+ catch ( FormatException )
787801 {
788802 // 转换失败,使用默认值
803+ // Conversion failed, use default value
804+ System . Diagnostics . Debug . WriteLine ( $ "属性 { property . Name } 的值 '{ stringValue } ' 转换为 { property . PropertyType . Name } 失败") ;
805+ }
806+ catch ( InvalidCastException )
807+ {
808+ // 类型转换失败,使用默认值
809+ // Type conversion failed, use default value
810+ System . Diagnostics . Debug . WriteLine ( $ "属性 { property . Name } 无法将值 '{ stringValue } ' 转换为 { property . PropertyType . Name } ") ;
789811 }
790812 }
791813 }
@@ -796,10 +818,29 @@ private void ApplyOptions(T target, Dictionary<string, object> options)
796818 property . SetValue ( target , convertedValue ) ;
797819 }
798820 }
799- catch ( Exception ex )
821+ catch ( ArgumentException ex )
822+ {
823+ // 属性设置参数错误,保持属性的默认状态
824+ // Property setting argument error, keep the property's default state
825+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 时发生参数错误: { ex . Message } ") ;
826+ }
827+ catch ( TargetInvocationException ex )
828+ {
829+ // 属性设置调用错误,保持属性的默认状态
830+ // Property setting invocation error, keep the property's default state
831+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 时发生调用错误: { ex . InnerException ? . Message ?? ex . Message } ") ;
832+ }
833+ catch ( FormatException ex )
834+ {
835+ // 格式转换错误,保持属性的默认状态
836+ // Format conversion error, keep the property's default state
837+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 时发生格式错误: { ex . Message } ") ;
838+ }
839+ catch ( InvalidCastException ex )
800840 {
801- Console . WriteLine ( $ "设置属性 { property . Name } 时发生错误 (An error occurred while setting property { property . Name } ): { ex . Message } ") ;
802- Console . WriteLine ( ex ) ;
841+ // 类型转换错误,保持属性的默认状态
842+ // Type conversion error, keep the property's default state
843+ System . Diagnostics . Debug . WriteLine ( $ "设置属性 { property . Name } 时发生类型转换错误: { ex . Message } ") ;
803844 }
804845 }
805846 }
0 commit comments