0

http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute.aspx

不可变类型作为配置属性

在 QueueID 下面的 QueueConfiguration 类中返回一个 int。当我运行代码时,访问 getter 时出现此错误:无法解析属性“queueID”的值。错误是:找不到支持“Int32”类型的属性“queueID”的字符串转换的转换器。

如果我更改 QueueID 以返回一个字符串,它工作正常。请注意,在上面引用的 microsoft 链接中,不需要类型转换器即可将端口属性作为 int 返回。我想我错过了一些明显的东西......

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; }
    }
}

这是我的 App.config (由于某种原因,该站点拒绝显示应用​​程序配置的 configSection 部分,所以我会尽力打破它:

<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>
4

1 回答 1

1

好吧,我只是复制粘贴并尝试了您的代码,它没有任何错误。我的阅读代码是:

var section = ConfigurationManager.GetSection("QueueConfigurations") as QueueConfigurationSection;
var queueId = section.Queues[0].QueueID;
Console.Out.WriteLine("queueId = {0}", queueId);

它打印queueId = 1

这是要点:https ://gist.github.com/b8499dcfa7456624f073

于 2012-09-19T23:16:11.190 回答