Custom Config Section: How to get a TimeSpan from an attribute value?

S

style

Hello

In my custom configuration section, I'd like to specify an interval
attribute which specifies an interval in milliseconds. I think it would be
very comfortable to get this value directly as a TimeSpan. That's what I
tryied:

[ConfigurationProperty("interval", IsRequired = true)]
public TimeSpan Interval
{
get { return (TimeSpan)this["interval"]; }
set { this["interval"] = value; }
}

I really got a TimeSpan, but unfortunately the value in the attribute is
automatically converted to days. Then I tried this:

[ConfigurationProperty("interval", IsRequired = true)]
public TimeSpan Interval
{
get { return TimeSpan.FromMilliseconds((double)this["interval"]); }
set { this["interval"] = value; }
}

But this did not work at all. Any other ideas how to achieve this?

Kind regards
Thomas
 
K

Kevin Spencer

A TimeSpan is not days, second, milliseconds, or any individual component of
the TimeSpan. The actual value of a TimeSpan is in Ticks, 100-nanosecond
intervals. You access the component days, hours, seconds, etc. by using the
various properties of the TimeSpan. For example, if you create a TimeSpan
from milliseconds, you can refer to the number of milliseconds by:

Interval.TotalMilliseconds

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.
 
H

Henning Krause [MVP - Exchange]

Hello,

add a

[TypeConverter(typeof(TimeSpanConverter))] attribute to the property.

Best regards,
Henning Krause
 
S

style

But adding the [TypeConverter(typeof(TimeSpanConverter))] attribute to the
property would force me to specifiy the interval within my custom
configuration section in ticks, wouldn't it? Or is there a way to define it
as milliseconds (or maybe seconds etc.)?

Thank you and have a nice weekend
 
H

Henning Krause [MVP - Exchange]

Hello,

actually, it would look like this:

[ConfigurationProperty("interval", IsRequired = true)]
[TypeConverter(typeof(TimeSpanConverter))]
public TimeSpan Interval
{
get { return (TimeSpan)this["interval"]; }
set { this["interval"] = value; }
}

In the configuration file, you specify it like this:

<element interval="1.00:00:00" />

this is just the ToString() representation of TimeSpan. So 1.00:00:00 is
exactly one day.

Best regards,
Henning Krause
 
S

style

Thank you for that information. But this still does not allow to set an
intervall in milliseconds. I found a sample of another way to implement a
TimeSpan property:
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationsection.aspx
As you can see, the property is initialized like this:

// The MaxIdleTime property.
private static readonly ConfigurationProperty _MaxIdleTime = new
ConfigurationProperty("maxIdleTime", typeof(TimeSpan),
TimeSpan.FromMinutes(5), ConfigurationPropertyOptions.IsRequired);

TimeSpan.FromMinutes(5) sets the default value. You could also set default
value of milliseconds by using TimeSpan.FromMilliseconds(100). But it seems
not to be possible to directly read milliseconds from the config file. What
a pitty.

Thank you and best wishes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top