22

似乎无法处理大于 23:59:59ConfigurationElement的值。TimeSpan有什么解决方法吗?子类化TimeSpan和制作新的TimeSpanValidatorAttribute甚至会起作用吗?我需要处理从几分钟到几天的时间跨度。

我正在使用以下配置部分

[ConfigurationProperty("SequenceRolloverDOSCompare", IsRequired = true)]
[TimeSpanValidator(MinValueString = "0:0:00", MaxValueString = 10675199.02:48:05.4775807", ExcludeRange = false)]
public TimeSpan SequenceRolloverDOSCompare
{
    get
    {
        return (TimeSpan)base["SequenceRolloverDOSCompare"];
    }
}

配置看起来像这样:

<SequenceRolloverPolling SequenceRolloverDOSCompare="2:00:00:00"  />

ConfigurationErrorsException : The value of the property 'SequenceRolloverDOSCompare' cannot be parsed. The error is: 2:00:00:00 is not a valid value for TimeSpan.

或这个:

<SequenceRolloverPolling SequenceRolloverDOSCompare="48:00:00"  />

OverflowException : The TimeSpan could not be parsed because at least one of the hours, minutes, or seconds components is outside its valid range

4

1 回答 1

34

.在天和小时之间使用分隔符:

<SequenceRolloverPolling
    SequenceRolloverDOSCompare="2.00:00:00" />

TimeSpan 格式定义为:

... [-]d.hh:mm:ss.ff,其中可选的减号表示负时间间隔,d 分量是天,hh 是按 24 小时制测量的小时,mm 是分钟,ss是秒,ff 是几分之一秒。

于 2010-06-30T00:21:11.083 回答