@@ -382,26 +382,26 @@ public void EmitRangeAttribute(string modifier, string prefix, string className,
382
382
383
383
string initializationString = emitTimeSpanSupport ?
384
384
"""
385
- if (OperandType == typeof(global::System.TimeSpan))
386
- {
387
- if (!global::System.TimeSpan.TryParse((string)Minimum, culture, out global::System.TimeSpan timeSpanMinimum) ||
388
- !global::System.TimeSpan.TryParse((string)Maximum, culture, out global::System.TimeSpan timeSpanMaximum))
389
- {
390
- throw new global::System.InvalidOperationException(c_minMaxError );
391
- }
392
- Minimum = timeSpanMinimum;
393
- Maximum = timeSpanMaximum;
394
- }
395
- else
396
- {
397
- Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(c_minMaxError );
398
- Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(c_minMaxError );
399
- }
385
+ if (OperandType == typeof(global::System.TimeSpan))
386
+ {
387
+ if (!global::System.TimeSpan.TryParse((string)Minimum, culture, out global::System.TimeSpan timeSpanMinimum) ||
388
+ !global::System.TimeSpan.TryParse((string)Maximum, culture, out global::System.TimeSpan timeSpanMaximum))
389
+ {
390
+ throw new global::System.InvalidOperationException(MinMaxError );
391
+ }
392
+ Minimum = timeSpanMinimum;
393
+ Maximum = timeSpanMaximum;
394
+ }
395
+ else
396
+ {
397
+ Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError );
398
+ Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError );
399
+ }
400
400
"""
401
401
:
402
402
"""
403
- Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(c_minMaxError );
404
- Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(c_minMaxError );
403
+ Minimum = ConvertValue(Minimum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError );
404
+ Maximum = ConvertValue(Maximum, culture) ?? throw new global::System.InvalidOperationException(MinMaxError );
405
405
""" ;
406
406
407
407
string convertValue = emitTimeSpanSupport ?
@@ -470,7 +470,7 @@ public void EmitRangeAttribute(string modifier, string prefix, string className,
470
470
public {{ qualifiedClassName }} (global::System.Type type, string minimum, string maximum) : base()
471
471
{
472
472
OperandType = type;
473
- NeedToConvertMinMax = true;
473
+ _needToConvertMinMax = true;
474
474
Minimum = minimum;
475
475
Maximum = maximum;
476
476
}
@@ -483,33 +483,40 @@ public void EmitRangeAttribute(string modifier, string prefix, string className,
483
483
public bool ConvertValueInInvariantCulture { get; set; }
484
484
public override string FormatErrorMessage(string name) =>
485
485
string.Format(global::System.Globalization.CultureInfo.CurrentCulture, GetValidationErrorMessage(), name, Minimum, Maximum);
486
- private bool NeedToConvertMinMax { get; }
487
- private bool Initialized { get; set; }
488
- private const string c_minMaxError = "The minimum and maximum values must be set to valid values.";
486
+ private readonly bool _needToConvertMinMax;
487
+ private volatile bool _initialized;
488
+ private readonly object _lock = new();
489
+ private const string MinMaxError = "The minimum and maximum values must be set to valid values.";
489
490
490
491
public override bool IsValid(object? value)
491
492
{
492
- if (!Initialized )
493
+ if (!_initialized )
493
494
{
494
- if (Minimum is null || Maximum is null)
495
- {
496
- throw new global::System.InvalidOperationException(c_minMaxError);
497
- }
498
- if (NeedToConvertMinMax)
495
+ lock (_lock)
499
496
{
500
- System.Globalization.CultureInfo culture = ParseLimitsInInvariantCulture ? global::System.Globalization.CultureInfo.InvariantCulture : global::System.Globalization.CultureInfo.CurrentCulture;
497
+ if (!_initialized)
498
+ {
499
+ if (Minimum is null || Maximum is null)
500
+ {
501
+ throw new global::System.InvalidOperationException(MinMaxError);
502
+ }
503
+ if (_needToConvertMinMax)
504
+ {
505
+ System.Globalization.CultureInfo culture = ParseLimitsInInvariantCulture ? global::System.Globalization.CultureInfo.InvariantCulture : global::System.Globalization.CultureInfo.CurrentCulture;
501
506
{{ initializationString }}
507
+ }
508
+ int cmp = ((global::System.IComparable)Minimum).CompareTo((global::System.IComparable)Maximum);
509
+ if (cmp > 0)
510
+ {
511
+ throw new global::System.InvalidOperationException("The maximum value '{Maximum}' must be greater than or equal to the minimum value '{Minimum}'.");
512
+ }
513
+ else if (cmp == 0 && (MinimumIsExclusive || MaximumIsExclusive))
514
+ {
515
+ throw new global::System.InvalidOperationException("Cannot use exclusive bounds when the maximum value is equal to the minimum value.");
516
+ }
517
+ _initialized = true;
518
+ }
502
519
}
503
- int cmp = ((global::System.IComparable)Minimum).CompareTo((global::System.IComparable)Maximum);
504
- if (cmp > 0)
505
- {
506
- throw new global::System.InvalidOperationException("The maximum value '{Maximum}' must be greater than or equal to the minimum value '{Minimum}'.");
507
- }
508
- else if (cmp == 0 && (MinimumIsExclusive || MaximumIsExclusive))
509
- {
510
- throw new global::System.InvalidOperationException("Cannot use exclusive bounds when the maximum value is equal to the minimum value.");
511
- }
512
- Initialized = true;
513
520
}
514
521
515
522
if (value is null or string { Length: 0 })
0 commit comments