Difficult Question: Assigning string default values to class/structConfigurationProperty attributes

K

Keller

Hello Group,

I posted this in microsoft.public.dotnet.general about a week ago but
haven't had any replies....

The Visual Studio 2005 help for
ConfigurationPropertyAttribute.DefaultValue
provides the following sample:

/////////////////////////////////////////////////
[ConfigurationProperty("maxIdleTime",
DefaultValue = "0:10:0",
IsRequired = false)]
[TimeSpanValidator(MinValueString = "0:0:30",
MaxValueString = "5:00:0",
ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}

/////////////////////////////////////////////////

What I am wondering is how does the DefaultValue parameter from the
sample code work, i.e.:

TimeSpan does not have a constructor or "=" operator that takes a
string parameter, and TimeSpan does not implement IConvertible. For
example, the following does not complile:

TimeSpan does have a Parse() method that takes a string parameter,
i.e.

but I do not see how it is associated in the above sample code.

So again how does this code from the sample work in regards to
assigning a string
DefaultValue to a TimeSpan value? i.e.:

My guess is TimeSpan.Parse() is somehow being called, but where?

TIA,
Keller Beyer
(e-mail address removed)
 
P

Pavel Minaev

I posted this in microsoft.public.dotnet.general about a week ago but
haven't had any replies....

The Visual Studio 2005 help for
ConfigurationPropertyAttribute.DefaultValue
provides the following sample:

/////////////////////////////////////////////////
[ConfigurationProperty("maxIdleTime",
    DefaultValue = "0:10:0",
    IsRequired = false)]
[TimeSpanValidator(MinValueString = "0:0:30",
    MaxValueString = "5:00:0",
    ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
    get
    {
        return (TimeSpan)this["maxIdleTime"];
    }
    set
    {
        this["maxIdleTime"] = value;
    }

}

/////////////////////////////////////////////////

What I am wondering is how does the DefaultValue parameter from the
sample code work, i.e.:

TimeSpan does not have a constructor or "=" operator that takes a
string parameter, and TimeSpan does not implement IConvertible.  For
example, the following does not complile:

TimeSpan does have a Parse() method that takes a string parameter,
i.e.

but I do not see how it is associated in the above sample code.

So again how does this code from the sample work in regards to
assigning a string
DefaultValue to a TimeSpan value? i.e.:

My guess is TimeSpan.Parse() is somehow being called, but where?

I am not familiar with this attribute in particular, but the general
way to do this sort of thing in .NET libraries is to use a type
converter. TimeSpan has an associated TimeSpanConverter, which
converts to/from string. It can be obtained using
TimeConverter.GetConverter(typeof(TimeSpan)), which is probably what
happens here.
 
K

Keller

I posted this in microsoft.public.dotnet.general about a week ago but
haven't had any replies....
The Visual Studio 2005 help for
ConfigurationPropertyAttribute.DefaultValue
provides the following sample:
/////////////////////////////////////////////////
[ConfigurationProperty("maxIdleTime",
    DefaultValue = "0:10:0",
    IsRequired = false)]
[TimeSpanValidator(MinValueString = "0:0:30",
    MaxValueString = "5:00:0",
    ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
    get
    {
        return (TimeSpan)this["maxIdleTime"];
    }
    set
    {
        this["maxIdleTime"] = value;
    }
/////////////////////////////////////////////////

What I am wondering is how does the DefaultValue parameter from the
sample code work, i.e.:
  DefaultValue = "0:10:0",
TimeSpan does not have a constructor or "=" operator that takes a
string parameter, and TimeSpan does not implement IConvertible.  For
example, the following does not complile:
TimeSpan does have a Parse() method that takes a string parameter,
i.e.
but I do not see how it is associated in the above sample code.
So again how does this code from the sample work in regards to
assigning a string
DefaultValue to a TimeSpan value? i.e.:
My guess is TimeSpan.Parse() is somehow being called, but where?

I am not familiar with this attribute in particular, but the general
way to do this sort of thing in .NET libraries is to use a type
converter. TimeSpan has an associated TimeSpanConverter, which
converts to/from string. It can be obtained using
TimeConverter.GetConverter(typeof(TimeSpan)), which is probably what
happens here.- Hide quoted text -

- Show quoted text -

Hi Pavel,
Thanks for the help and info regarding TimeSpanConverter. I will post
what I learn about how this example works.
Keller
 
K

Keller

[...]
  DefaultValue = "0:10:0",
My guess is TimeSpan.Parse() is somehow being called, but where?

Like Pavel, I don't have a specific answer for you.  But, note that you 
can inspect the .NET implementation yourself and see how it works.  With  
Red Gate's Reflector, you can browse around the .NET DLLs.  An easier way  
is to enable source-code download in Visual Studio, set a break-point on  
the property's setter, and then see what led up to the default value being  
set.

Note that TimeSpan is one of five special-cased types supported for  
configuration elements (Int32, Int64, regex String, and plain String being  
the other four).  So it wouldn't surprise me too much to see some code in  
the configuration-management implementation that simply special-cases the 
type and performs the necessary Parse() (or possibly the TimeSpanConverter  
as Pavel suggests).

Pete

Hi Pete,
Thanks for the help and info about the special-cased types.

I did set a break point on the property's setter, but I don't think it
gets called (I will double check tonight). i.e.:

public class ConfigurationSection :
System.Configuration.ConfigurationSection
{
[ConfigurationProperty("maxDateTime", DefaultValue =
"0:10:0")]
public System.DateTime MaxDateTime
{
get { return (System.DateTime)this
["maxDateTime"]; }
set { this["maxDateTime"] = value; } // << Does
not appear to be called by constructor to set DefaultValue
}

[ConfigurationProperty("maxIdleTime", DefaultValue =
"0:10:0")]
public TimeSpan MaxIdleTime
{
get { return (TimeSpan)this["maxIdleTime"]; }
set { this["maxIdleTime"] = value; } // << Does
not appear to be called by constructor to set DefaultValue
}
}

I have been meaning to install Reflector, but I want to do a drive
image of my system first (I also have the
mobile SDK installed). I also plan to install the "Configuration
Section Designer" tool available on CodePlex,
i.e. http://csd.codeplex.com/.

Thanks again for the reply, I will post what I learn about setting
DefaultValues.
Keller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top