@@ -54,6 +54,8 @@ public class StrategyParam<T> : NotifiableObject, IStrategyParam
5454 private T _stepValue ;
5555 private T _stepBaseValue ;
5656
57+ private static readonly Type _valueType = typeof ( T ) . GetUnderlyingType ( ) ?? typeof ( T ) ;
58+
5759 /// <summary>
5860 /// Initializes a new instance of the <see cref="StrategyParam{T}"/>.
5961 /// </summary>
@@ -89,7 +91,7 @@ public T Value
8991 if ( ! this . IsValid ( value ) )
9092 throw new ArgumentOutOfRangeException ( nameof ( value ) , value , LocalizedStrings . InvalidValue ) ;
9193
92- if ( _hasStep && _hasStep && ! IsValueMatchesStep ( GetValueType ( ) , value , _stepValue , _stepBaseValue ) )
94+ if ( _hasStep && _hasStep && ! IsValueMatchesStep ( _valueType , value , _stepValue , _stepBaseValue ) )
9395 throw new ArgumentOutOfRangeException ( nameof ( value ) , value , LocalizedStrings . InvalidValue ) ;
9496
9597 _value = value ;
@@ -172,7 +174,7 @@ private static bool IsValueMatchesStep(Type type, T value, T step, T baseValue)
172174 /// <returns><see cref="StrategyParam{T}"/>.</returns>
173175 public StrategyParam < T > SetStep ( T step , T baseValue = default )
174176 {
175- var type = GetValueType ( ) ;
177+ var type = _valueType ;
176178
177179 bool invalid ;
178180
@@ -288,36 +290,24 @@ public StrategyParam<T> SetBasic(bool basic = true)
288290 public StrategyParam < T > SetReadOnly ( bool value = true )
289291 => ModifyAttributes ( value , ( ) => new ReadOnlyAttribute ( true ) ) ;
290292
291- private static Type GetValueType ( )
292- {
293- var type = typeof ( T ) ;
294- return type . GetUnderlyingType ( ) ?? type ;
295- }
296-
297293 /// <summary>
298294 /// Set greater than zero validator.
299295 /// </summary>
300296 /// <returns><see cref="StrategyParam{T}"/></returns>
301297 public StrategyParam < T > SetGreaterThanZero ( )
302298 {
303- var type = GetValueType ( ) ;
304-
305- ValidationAttribute attr ;
306-
307- if ( type == typeof ( int ) )
308- attr = new IntGreaterThanZeroAttribute ( ) ;
309- else if ( type == typeof ( long ) )
310- attr = new LongGreaterThanZeroAttribute ( ) ;
311- else if ( type == typeof ( decimal ) )
312- attr = new DecimalGreaterThanZeroAttribute ( ) ;
313- else if ( type == typeof ( double ) )
314- attr = new DoubleGreaterThanZeroAttribute ( ) ;
315- else if ( type == typeof ( float ) )
316- attr = new FloatGreaterThanZeroAttribute ( ) ;
317- else if ( type == typeof ( TimeSpan ) )
318- attr = new TimeSpanGreaterThanZeroAttribute ( ) ;
319- else
320- throw new InvalidOperationException ( type . Name ) ;
299+ var type = _valueType ;
300+
301+ ValidationAttribute attr = type switch
302+ {
303+ _ when type == typeof ( int ) => new IntGreaterThanZeroAttribute ( ) ,
304+ _ when type == typeof ( long ) => new LongGreaterThanZeroAttribute ( ) ,
305+ _ when type == typeof ( decimal ) => new DecimalGreaterThanZeroAttribute ( ) ,
306+ _ when type == typeof ( double ) => new DoubleGreaterThanZeroAttribute ( ) ,
307+ _ when type == typeof ( float ) => new FloatGreaterThanZeroAttribute ( ) ,
308+ _ when type == typeof ( TimeSpan ) => new TimeSpanGreaterThanZeroAttribute ( ) ,
309+ _ => throw new InvalidOperationException ( type . Name )
310+ } ;
321311
322312 return this . SetValidator ( attr ) ;
323313 }
@@ -328,24 +318,18 @@ public StrategyParam<T> SetGreaterThanZero()
328318 /// <returns><see cref="StrategyParam{T}"/></returns>
329319 public StrategyParam < T > SetNullOrMoreZero ( )
330320 {
331- var type = GetValueType ( ) ;
332-
333- ValidationAttribute attr ;
334-
335- if ( type == typeof ( int ) )
336- attr = new IntNullOrMoreZeroAttribute ( ) ;
337- else if ( type == typeof ( long ) )
338- attr = new LongNullOrMoreZeroAttribute ( ) ;
339- else if ( type == typeof ( decimal ) )
340- attr = new DecimalNullOrMoreZeroAttribute ( ) ;
341- else if ( type == typeof ( double ) )
342- attr = new DoubleNullOrMoreZeroAttribute ( ) ;
343- else if ( type == typeof ( float ) )
344- attr = new FloatNullOrMoreZeroAttribute ( ) ;
345- else if ( type == typeof ( TimeSpan ) )
346- attr = new TimeSpanNullOrMoreZeroAttribute ( ) ;
347- else
348- throw new InvalidOperationException ( type . Name ) ;
321+ var type = _valueType ;
322+
323+ ValidationAttribute attr = type switch
324+ {
325+ _ when type == typeof ( int ) => new IntNullOrMoreZeroAttribute ( ) ,
326+ _ when type == typeof ( long ) => new LongNullOrMoreZeroAttribute ( ) ,
327+ _ when type == typeof ( decimal ) => new DecimalNullOrMoreZeroAttribute ( ) ,
328+ _ when type == typeof ( double ) => new DoubleNullOrMoreZeroAttribute ( ) ,
329+ _ when type == typeof ( float ) => new FloatNullOrMoreZeroAttribute ( ) ,
330+ _ when type == typeof ( TimeSpan ) => new TimeSpanNullOrMoreZeroAttribute ( ) ,
331+ _ => throw new InvalidOperationException ( type . Name )
332+ } ;
349333
350334 return this . SetValidator ( attr ) ;
351335 }
@@ -356,24 +340,18 @@ public StrategyParam<T> SetNullOrMoreZero()
356340 /// <returns><see cref="StrategyParam{T}"/></returns>
357341 public StrategyParam < T > SetNullOrNotNegative ( )
358342 {
359- var type = GetValueType ( ) ;
360-
361- ValidationAttribute attr ;
362-
363- if ( type == typeof ( int ) )
364- attr = new IntNullOrNotNegativeAttribute ( ) ;
365- else if ( type == typeof ( long ) )
366- attr = new LongNullOrNotNegativeAttribute ( ) ;
367- else if ( type == typeof ( decimal ) )
368- attr = new DecimalNullOrNotNegativeAttribute ( ) ;
369- else if ( type == typeof ( double ) )
370- attr = new DoubleNullOrNotNegativeAttribute ( ) ;
371- else if ( type == typeof ( float ) )
372- attr = new FloatNullOrNotNegativeAttribute ( ) ;
373- else if ( type == typeof ( TimeSpan ) )
374- attr = new TimeSpanNullOrNotNegativeAttribute ( ) ;
375- else
376- throw new InvalidOperationException ( type . Name ) ;
343+ var type = _valueType ;
344+
345+ ValidationAttribute attr = type switch
346+ {
347+ _ when type == typeof ( int ) => new IntNullOrNotNegativeAttribute ( ) ,
348+ _ when type == typeof ( long ) => new LongNullOrNotNegativeAttribute ( ) ,
349+ _ when type == typeof ( decimal ) => new DecimalNullOrNotNegativeAttribute ( ) ,
350+ _ when type == typeof ( double ) => new DoubleNullOrNotNegativeAttribute ( ) ,
351+ _ when type == typeof ( float ) => new FloatNullOrNotNegativeAttribute ( ) ,
352+ _ when type == typeof ( TimeSpan ) => new TimeSpanNullOrNotNegativeAttribute ( ) ,
353+ _ => throw new InvalidOperationException ( type . Name )
354+ } ;
377355
378356 return this . SetValidator ( attr ) ;
379357 }
@@ -384,24 +362,18 @@ public StrategyParam<T> SetNullOrNotNegative()
384362 /// <returns><see cref="StrategyParam{T}"/></returns>
385363 public StrategyParam < T > SetNotNegative ( )
386364 {
387- var type = GetValueType ( ) ;
388-
389- ValidationAttribute attr ;
390-
391- if ( type == typeof ( int ) )
392- attr = new IntNotNegativeAttribute ( ) ;
393- else if ( type == typeof ( long ) )
394- attr = new LongNotNegativeAttribute ( ) ;
395- else if ( type == typeof ( decimal ) )
396- attr = new DecimalNotNegativeAttribute ( ) ;
397- else if ( type == typeof ( double ) )
398- attr = new DoubleNotNegativeAttribute ( ) ;
399- else if ( type == typeof ( float ) )
400- attr = new FloatNotNegativeAttribute ( ) ;
401- else if ( type == typeof ( TimeSpan ) )
402- attr = new TimeSpanNotNegativeAttribute ( ) ;
403- else
404- throw new InvalidOperationException ( type . Name ) ;
365+ var type = _valueType ;
366+
367+ ValidationAttribute attr = type switch
368+ {
369+ _ when type == typeof ( int ) => new IntNotNegativeAttribute ( ) ,
370+ _ when type == typeof ( long ) => new LongNotNegativeAttribute ( ) ,
371+ _ when type == typeof ( decimal ) => new DecimalNotNegativeAttribute ( ) ,
372+ _ when type == typeof ( double ) => new DoubleNotNegativeAttribute ( ) ,
373+ _ when type == typeof ( float ) => new FloatNotNegativeAttribute ( ) ,
374+ _ when type == typeof ( TimeSpan ) => new TimeSpanNotNegativeAttribute ( ) ,
375+ _ => throw new InvalidOperationException ( type . Name )
376+ } ;
405377
406378 return this . SetValidator ( attr ) ;
407379 }
@@ -412,24 +384,18 @@ public StrategyParam<T> SetNotNegative()
412384 /// <returns><see cref="StrategyParam{T}"/></returns>
413385 public StrategyParam < T > SetPositive ( )
414386 {
415- var type = GetValueType ( ) ;
416-
417- object max ;
418-
419- if ( type == typeof ( int ) )
420- max = int . MaxValue ;
421- else if ( type == typeof ( long ) )
422- max = long . MaxValue ;
423- else if ( type == typeof ( decimal ) )
424- max = decimal . MaxValue ;
425- else if ( type == typeof ( double ) )
426- max = double . MaxValue ;
427- else if ( type == typeof ( float ) )
428- max = float . MaxValue ;
429- else if ( type == typeof ( TimeSpan ) )
430- max = TimeSpan . MaxValue ;
431- else
432- throw new InvalidOperationException ( type . Name ) ;
387+ var type = _valueType ;
388+
389+ object max = type switch
390+ {
391+ _ when type == typeof ( int ) => int . MaxValue ,
392+ _ when type == typeof ( long ) => long . MaxValue ,
393+ _ when type == typeof ( decimal ) => decimal . MaxValue ,
394+ _ when type == typeof ( double ) => double . MaxValue ,
395+ _ when type == typeof ( float ) => float . MaxValue ,
396+ _ when type == typeof ( TimeSpan ) => TimeSpan . MaxValue ,
397+ _ => throw new InvalidOperationException ( type . Name )
398+ } ;
433399
434400 return SetRange ( 1L . To < T > ( ) , max . To < T > ( ) ) ;
435401 }
@@ -442,14 +408,11 @@ public StrategyParam<T> SetPositive()
442408 /// <returns><see cref="StrategyParam{T}"/></returns>
443409 public StrategyParam < T > SetRange ( T min , T max )
444410 {
445- var type = GetValueType ( ) ;
446-
447- RangeAttribute attr ;
411+ var type = _valueType ;
448412
449- if ( type == typeof ( int ) )
450- attr = new ( min . To < int > ( ) , max . To < int > ( ) ) ;
451- else
452- attr = new ( type , min . To < string > ( ) , max . To < string > ( ) ) ;
413+ RangeAttribute attr = type == typeof ( int )
414+ ? new RangeAttribute ( min . To < int > ( ) , max . To < int > ( ) )
415+ : new RangeAttribute ( type , min . To < string > ( ) , max . To < string > ( ) ) ;
453416
454417 return this . SetValidator ( attr ) ;
455418 }
@@ -470,9 +433,7 @@ private StrategyParam<T> ModifyAttributes<TAttr>(bool add, Func<TAttr> create)
470433 /// </summary>
471434 /// <returns><see cref="StrategyParam{T}"/></returns>
472435 public StrategyParam < T > SetRequired ( )
473- {
474- return Ecng . ComponentModel . Extensions . SetRequired ( this ) ;
475- }
436+ => Ecng . ComponentModel . Extensions . SetRequired ( this ) ;
476437
477438 private static class Keys
478439 {
0 commit comments