473,545 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

[ConfigurationPr operty("interva l", 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:

[ConfigurationPr operty("interva l", IsRequired = true)]
public TimeSpan Interval
{
get { return TimeSpan.FromMi lliseconds((dou ble)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 9773
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.TotalM illiseconds

--
HTH,

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

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

"style" <!s**********@m y-mail.chwrote in message
news:ei******** ******@TK2MSFTN GP02.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:

[ConfigurationPr operty("interva l", 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:

[ConfigurationPr operty("interva l", IsRequired = true)]
public TimeSpan Interval
{
get { return TimeSpan.FromMi lliseconds((dou ble)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(t ypeof(TimeSpanC onverter))] attribute to the property.

Best regards,
Henning Krause

"style" <!s**********@m y-mail.chwrote in message
news:ei******** ******@TK2MSFTN GP02.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:

[ConfigurationPr operty("interva l", 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:

[ConfigurationPr operty("interva l", IsRequired = true)]
public TimeSpan Interval
{
get { return TimeSpan.FromMi lliseconds((dou ble)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(t ypeof(TimeSpanC onverter))] 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:

[ConfigurationPr operty("interva l", IsRequired = true)]
[TypeConverter(t ypeof(TimeSpanC onverter))]
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**********@m y-mail.chwrote in message
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
But adding the [TypeConverter(t ypeof(TimeSpanC onverter))] 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 ConfigurationPr operty _MaxIdleTime = new
ConfigurationPr operty("maxIdle Time", typeof(TimeSpan ),
TimeSpan.FromMi nutes(5), ConfigurationPr opertyOptions.I sRequired);

TimeSpan.FromMi nutes(5) sets the default value. You could also set default
value of milliseconds by using TimeSpan.FromMi lliseconds(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
1830
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 ... for a while. At random points in time my section handler stops working and throws the exception: Configuration Error Description: An error...
4
1907
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"> <site url="/blog/"> <mode value="live">
6
3788
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" type="ComIT.Applications.Common.ConfigurationSections.MyHandler" allowLocation="true" allowDefinition="Everywhere" /> </configSections>
3
1585
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. It works fine except for one thing that the custom configuration file has a poor change awareness. Custom configuration settings can not be...
0
1244
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" encoding="utf-8" ?> <configuration> <configSections> <section name="Employees" type="CustomConfigurationTest.MyConfigSection, CustomConfigurationTest"...
6
4791
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 install the dll to the same directory as the exe which uses it, everything works as expected but if I load the dll in any other directory, I get the...
2
5095
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 = Windows Forms class B = a singleton hosted within A. B is responsible for dynamically loading classes X, Y, and Z.
1
5922
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). However, my section doesn't seem to be recognized (GetSection("ReportServerUrls") returns null). I don't really see how what I'm doing differs from...
2
3775
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 case- sensitive. (C:\Documents and Settings\Miguel\My Documents\Website \web.config line 42)
0
7470
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7659
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7760
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5334
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.