By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.