首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将ConfigurationProperty转换为整数

无法将ConfigurationProperty转换为整数
EN

Stack Overflow用户
提问于 2012-09-20 05:47:48
回答 1查看 1.9K关注 0票数 0

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

Immutable types as configuration properties

在下面的QueueConfiguration类中,QueueID返回一个整数。当我运行代码时,我在访问getter时得到这个错误:无法解析属性'queueID‘的值。错误是:找不到支持与类型为“”int32“”的属性“”queueID“”相互转换的字符串的转换器。“”

如果我将QueueID更改为返回一个字符串,它会工作得很好。请注意,在上面引用的microsoft链接中,不需要使用类型转换器将port属性作为int返回。我想我漏掉了一些明显的东西......

代码语言:javascript
复制
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部分,所以我将尽最大努力打破它:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-20 07:16:11

好吧,我只是复制-粘贴并尝试了你的代码,它工作起来没有任何错误。我读到的代码是:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12503398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档