|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Custom Config Section: How to get a TimeSpan from an attribute value?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 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 -- Show quoteHTH, Kevin Spencer Microsoft MVP Bit Player http://unclechutney.blogspot.com Where there's a Will, there's a William. "style" <!spam.g.host@my-mail.ch> wrote in message news:eiNEtxKMHHA.3424@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 > > > Hello,
add a [TypeConverter(typeof(TimeSpanConverter))] attribute to the property. Best regards, Henning Krause Show quote "style" <!spam.g.host@my-mail.ch> wrote in message news:eiNEtxKMHHA.3424@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 > > > 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 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 Show quote "style" <!spam.g.host@my-mail.ch> wrote in message news:%23xW6I6OMHHA.1044@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 > > 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. |
|||||||||||||||||||||||