469,338 Members | 8,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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

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

Jan 5 '07 #1
5 9256
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.

"style" <!s**********@my-mail.chwrote in message
news:ei**************@TK2MSFTNGP02.phx.gbl...
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

Jan 5 '07 #2
Hello,

add a

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

Best regards,
Henning Krause

"style" <!s**********@my-mail.chwrote in message
news:ei**************@TK2MSFTNGP02.phx.gbl...
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
Jan 5 '07 #3
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
Jan 5 '07 #4
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

"style" <!s**********@my-mail.chwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Jan 6 '07 #5
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/lib...onsection.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.
Jan 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Søren Lund | last post: by
4 posts views Thread by Nick Gilbert | last post: by
6 posts views Thread by Tabi | last post: by
6 posts views Thread by Jeff Hegedus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.