先前的答案是正确的,但我也会提供所有代码。
您的app.config应该如下所示:
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <section name="ServicesSection" type="RT.Core.Config.ServiceConfigurationSection, RT.Core"/> </configSections> <ServicesSection> <Services> <add Port="6996" ReportType="File" /> <add Port="7001" ReportType="Other" /> </Services> </ServicesSection></configuration>
您的
ServiceConfig和
ServiceCollection类保持不变。
您需要一门新课:
public class ServiceConfigurationSection : ConfigurationSection{ [ConfigurationProperty("Services", IsDefaultCollection = false)] [ConfigurationCollection(typeof(ServiceCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")] public ServiceCollection Services { get { return (ServiceCollection)base["Services"]; } }}这应该可以解决问题。要使用它,您可以使用:
ServiceConfigurationSection serviceConfigSection = ConfigurationManager.GetSection("ServicesSection") as ServiceConfigurationSection;ServiceConfig serviceConfig = serviceConfigSection.Services[0];


