.net 无法将ConfigurationProperty转换为int

.net 无法将ConfigurationProperty转换为int,.net,.net,在QueueConfiguration类中,QueueID下面返回一个int。当我运行代码时,访问getter时会出现以下错误: 无法分析属性“queueID”的值。错误是:找不到支持与类型为“Int32”的属性“queueID”的字符串进行转换的转换器 如果我更改QueueID以返回字符串,则可以正常工作。请注意,在上面引用的microsoft链接中,不需要使用typeconverter将端口属性作为int返回。我想我遗漏了一些明显的东西 public class QueueConfigura

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

如果我更改QueueID以返回字符串,则可以正常工作。请注意,在上面引用的microsoft链接中,不需要使用typeconverter将端口属性作为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(由于某些原因,该网站拒绝显示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>

嗯,我只是复制粘贴并尝试了你的代码,它工作正常,没有任何错误。我的阅读代码是:

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


要点如下:

怎么了?这让我发疯了。我删除了bin和obj的内容,并清理了项目(希望它是配置文件的残余)。我仍然收到此错误:无法分析属性“queueID”的值。错误是:找不到支持与类型为“Int32”的属性“queueID”的字符串进行转换的转换器。(C:\yada\bin\Debug\STPMonitor.vshost.exe.Config行229)这是第229行:顺便说一句,谢谢您的回复!!您使用什么.NET版本?您使用什么代码读取配置?我正在使用VS 2010。读取配置文件的代码是:List queueList=new List();QueueConfigurationCollection queues=((QueueConfigurationSection)ConfigurationManager.GetSection(“QueueConfigurations”)).Queues;foreach(QueueConfiguration q in Queues){queueList.Add(q);}这是我见过的最疯狂的事情。我将我的代码verbatum复制到一个新项目中,它工作正常。我的vs项目有问题。如果这解决了你的问题,我建议