Tôi biết mình đã trễ nhưng tôi đã tìm thấy giải pháp này và nó hoạt động hoàn hảo vì vậy tôi chỉ muốn chia sẻ.
Đó là tất cả về việc xác định của riêng 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"]; }
}
}
}
Và trong app.config chỉ cần sử dụng phần mới của bạn:
<configSections>
<section name="PreRequest" type="Configuration.Helpers.MultipleValuesSection,
Configuration.Helpers" requirePermission="false" />
</configSections>
<PreRequest>
<Values>
<add name="C++"/>
<add name="Some Application"/>
</Values>
</PreRequest>
và khi lấy dữ liệu chỉ như thế này:
var section = (MultipleValuesSection) ConfigurationManager.GetSection("PreRequest");
var applications = (from object value in section.Values
select ((ValueElement)value).Name)
.ToList();
Cuối cùng, nhờ tác giả của bản gốc post
Nguồn
2015-04-07 08:29:58
Vì vậy, các điểm của 'ConfigurationManager.AppSettings.GetValues (là những gì) 'sau đó? – Yuck
@Yuck một câu hỏi về điểm của lớp NameValueCollection cơ bản - hỗ trợ nhiều giá trị cho mỗi khóa, nhưng không thực sự cho phép bạn đặt nhiều giá trị cho mỗi khóa (AppSettings phải sử dụng bộ chỉ mục) - đây là nguyên nhân thực sự của vấn đề, thay vì GetValues () chỉ trả về một giá trị duy nhất. – fusi
Nếu chỉ có một giá trị duy nhất, bất kỳ lỗi nào không tìm thấy ký tự xảy ra? –