473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 9755
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Søren Lund | last post by:
Hello, I have implemented a custom config section handler by implementing the IConfigurationSectionHandler interface. I have registered this handler in web.config and everything works fine ......
4
by: Nick Gilbert | last post by:
Hi, I would like the ability to store the configuration settings for all versions of my site in a single web.config file by using different sections. Eg: <siteConfig> <machine name="XENON">...
6
by: Tabi | last post by:
Hi, I want to create a custom section in my web.config that can hold my custom values. I created a section in web.config as written below. <configSections> <section name="myCustomSection"...
3
by: Laser Lu | last post by:
Hello, everybody, In order to avoid making web.config lengthy, I just moved some custom configuration settings out from web.config, and centralized them into an xml file, which names data.config....
0
by: style | last post by:
In the following sample I declared a custom configuration section within my app.config. As you can see, the lastName attribute of the second employee element is missing: <?xml version="1.0"...
6
by: Jeff Hegedus | last post by:
I have a dll that requires some configuration data. I put the configuration data in a custom configuration section in a config file that is loaded in the installation folder of the dll. If I...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
1
by: mcstrini | last post by:
I'm trying to have my application use a special config file which is shared across servers, and want to include a custom section (for URLs of report services associated with database instances). ...
2
by: shapper | last post by:
Hello, On a custom Web.Config section I keep having the same error. This is driving me crazy!!! I keep having the following error: Unrecognized attribute 'Type'. Note that attribute names are...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.