So che sono in ritardo ma ho trovato questa soluzione e funziona perfettamente, quindi voglio solo condividere.
E 'tutto su come definire il proprio ConfigurationElement
namespace Configuration.Helpers
{
public class ValueElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string) this["name"]; }
}
}
public class ValueElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ValueElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ValueElement)element).Name;
}
}
public class MultipleValuesSection : ConfigurationSection
{
[ConfigurationProperty("Values")]
public ValueElementCollection Values
{
get { return (ValueElementCollection)this["Values"]; }
}
}
}
E in app.config basta usare la tua nuova sezione:
<configSections>
<section name="PreRequest" type="Configuration.Helpers.MultipleValuesSection,
Configuration.Helpers" requirePermission="false" />
</configSections>
<PreRequest>
<Values>
<add name="C++"/>
<add name="Some Application"/>
</Values>
</PreRequest>
e quando il recupero dei dati, proprio come questo:
var section = (MultipleValuesSection) ConfigurationManager.GetSection("PreRequest");
var applications = (from object value in section.Values
select ((ValueElement)value).Name)
.ToList();
Infine, grazie all'autore dell'originale post
fonte
2015-04-07 08:29:58
Allora, qual è il punto di 'ConfigurationManager.AppSettings.GetValues () 'allora? – Yuck
@Yuck one interroga il punto della classe NameValueCollection sottostante, che supporta più valori per chiave, ma in realtà non consente di impostare più di una chiave (AppSettings deve utilizzare internamente l'indicizzatore dell'insieme) - questa è la vera causa del problema, piuttosto che GetValues () restituisce solo un singolo valore. – fusi
Se è presente un solo valore, si verifica un errore di carattere non trovato? –