http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute.aspx
Immutable types as configuration properties
In the QueueConfiguration class below QueueID returns an int. When I run the code I get this error when accessing the getter:
The value of the property ‘queueID’ cannot be parsed. The error is: Unable to find a converter that supports conversion to/from string for the property ‘queueID’ of type ‘Int32’.
If I change QueueID to return a string it works fine. Note in the microsoft link cited above that a typeconverter is not needed to return the port property as an int. I’m suppose I’m missing something obvious……
public class QueueConfiguration : ConfigurationSection
{
[ConfigurationProperty("queueID", DefaultValue = (int)0, IsKey = true, IsRequired = true)]
public int QueueID
{
get
{
return (int)this["queueID"];
}
set { this["queueID"] = value; }
}
[ConfigurationProperty("queueName", DefaultValue = "", IsKey = false, IsRequired = true)]
public string QueueName
{
get { return (string)this["queueName"]; }
set { this["queueName"] = value; }
}
}
public class QueueConfigurationCollection : ConfigurationElementCollection
{
internal const string PropertyName = "QueueConfiguration";
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMapAlternate;
}
}
protected override string ElementName
{
get
{
return PropertyName;
}
}
protected override bool IsElementName(string elementName)
{
return elementName.Equals(PropertyName, StringComparison.InvariantCultureIgnoreCase);
}
public override bool IsReadOnly()
{
return false;
}
protected override ConfigurationElement CreateNewElement()
{
return new QueueConfiguration();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((QueueConfiguration)(element)).QueueID;
}
public QueueConfiguration this[int idx]
{
get
{
return (QueueConfiguration)BaseGet(idx);
}
}
}
public class QueueConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Queues")]
public QueueConfigurationCollection Queues
{
get { return ((QueueConfigurationCollection)(this["Queues"])); }
set { this["Queues"] = value; }
}
}
Here is my App.config (for some reason this site refuses to display the configSection portion of app config so I’ll do my best to break it:
<configSections>
<section name="QueueConfigurations" type="STPMonitor.Common.QueueConfigurationSection, STPMonitor"/>
</configSections>
<QueueConfigurations>
<Queues>
<QueueConfiguration queueID="1" queueName="One"></QueueConfiguration>
<QueueConfiguration queueID="2" queueName="Two"></QueueConfiguration>
</Queues>
</QueueConfigurations>
Well, I just copy-pasted and tried your code and it works without any errors. My read code was:
and it prints
queueId = 1here is gist: https://gist.github.com/b8499dcfa7456624f073