gpt4 book ai didi

c# - 自定义配置集合 - 无法识别的元素 'addService'

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:21 28 4
gpt4 key购买 nike

来自 MSDN 的关于制作自定义配置部分的示例应该如下所示,

class RemoteServiceSection : ConfigurationSection
{
[ConfigurationProperty("remoteServices", IsDefaultCollection=false)]
[ConfigurationCollection(typeof(RemoteServiceCollection), AddItemName="addService", ClearItemsName="clearServices",
RemoveItemName="removeService")]
public RemoteServiceCollection Services
{
get
{
return this["remoteServices"] as RemoteServiceCollection;
}
}
}

class RemoteServiceCollection : ConfigurationElementCollection, IList<RemoteServiceElement>
{
public RemoteServiceCollection()
{
RemoteServiceElement element = (RemoteServiceElement)CreateNewElement();
Add(element);
}

public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}

protected override ConfigurationElement CreateNewElement()
{
return new RemoteServiceElement();
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((RemoteServiceElement)element).Hostname;
}

protected override string ElementName
{
get
{
return "remoteService";
}
}

public new IEnumerator<RemoteServiceElement> GetEnumerator()
{
foreach (RemoteServiceElement element in this)
{
yield return element;
}
}

public void Add(RemoteServiceElement element)
{
BaseAdd(element, true);
}

public void Clear()
{
BaseClear();
}

public bool Contains(RemoteServiceElement element)
{
return !(BaseIndexOf(element) < 0);
}

public void CopyTo(RemoteServiceElement[] array, int index)
{
base.CopyTo(array, index);
}

public bool Remove(RemoteServiceElement element)
{
BaseRemove(GetElementKey(element));
return true;
}

bool ICollection<RemoteServiceElement>.IsReadOnly
{
get { return IsReadOnly(); }
}

public int IndexOf(RemoteServiceElement element)
{
return BaseIndexOf(element);
}

public void Insert(int index, RemoteServiceElement element)
{
BaseAdd(index, element);
}

public void RemoveAt(int index)
{
BaseRemoveAt(index);
}

public RemoteServiceElement this[int index]
{
get
{
return (RemoteServiceElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
}

class RemoteServiceElement : ConfigurationElement
{
public RemoteServiceElement() { }

public RemoteServiceElement(string ip, string port)
{
this.IpAddress = ip;
this.Port = port;
}

[ConfigurationProperty("hostname", IsKey = true, IsRequired = true)]
public string Hostname
{
get
{
return (string)this["hostname"];
}
set
{
this["hostname"] = value;
}
}
[ConfigurationProperty("ipAddress", IsRequired = true)]
public string IpAddress
{
get
{
return (string)this["ipAddress"];
}
set
{
this["ipAddress"] = value;
}
}
[ConfigurationProperty("port", IsRequired = true)]
public string Port
{
get
{
return (string)this["port"];
}
set
{
this["port"] = value;
}
}
}

我收到“无法识别的元素‘addService’”的错误。我想我完全遵循了 MSDN 文章。可以在这里找到 - http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

在此先感谢您的帮助。这是我在 app.config 中写的(当然这里没有显示括号?):

 <remoteServices>
<addService hostname="xxxxxxx" ipAddress="xxx.x.xxx.xx" port="xxxx" >
</remoteServices>

这里是 app.config 的要求,只是出于隐私目的 x' 出特定名称,它们只是字符串:

<configuration>
<configSections>
<section name="remoteServices" type="AqEntityTests.RemoteServiceSection,
AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
<remoteServices>
<addService hostname="xxxxxx.xxxxxxx.com"
ipAddress="xxx.x.xxx.xx"
port="xx" />
</remoteServices>

最佳答案

为了子孙后代:

你的配置应该是这样的:

<configuration>
<configSections>
<section name="remoteServices" type="AqEntityTests.RemoteServiceSection,
AqEntityTests" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>

<remoteServices>
<remoteServices>
<addService hostname="xxxxxx.xxxxxxx.com"
ipAddress="xxx.x.xxx.xx"
port="xx" />
</remoteServices>
</remoteServices>
</configuration>

为什么?

你添加到节点:

<configSections> 

自定义部分命名为:

name="remoteServices"

有类型

type="AqEntityTests.RemoteServiceSection

然后在代码中,将属性添加到自定义部分:

[ConfigurationProperty("remoteServices", IsDefaultCollection=false)]

意思是你在节点内创建了节点,两者同名。因此,您收到错误“无法识别的元素‘addService’”。只是编译器通知你这样的元素不应该在那个节点中。

自定义配置快速学习的两个链接:
Custom Configuration Sections for Lazy Coders
How to create sections with collections

关于c# - 自定义配置集合 - 无法识别的元素 'addService',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988375/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com