App.config:
<configuration> <configSections> <section name = "PokerClients" type = "WindowsFormsApplication1.PokerClientsConfig"/> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <PokerClients> <PokerClient key="PokerStars" name ="Poker stars" wndclass = "PokerStarsTableFrameClass"/> <PokerClient key="poker" name ="888 Poker" wndclass = "Poker888StarsTableFrameClass"/> </PokerClients> </appSettings> </configuration> here is the implementation (taken from here ).
namespace WindowsFormsApplication1 //"модуль формы" { public class PokerClient : ConfigurationElement { [ConfigurationProperty("key", IsRequired = true)] public string Key { get { return this["key"] as string; } } [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return this["name"] as string; } } [ConfigurationProperty("wndclass", IsRequired = true)] public string Wndclass { get { return this["wndclass"] as string; } } } public class PokerClients : ConfigurationElementCollection { public PokerClient this[int index] { get { return base.BaseGet(index) as PokerClient; } set { if (base.BaseGet(index) != null) { base.BaseRemoveAt(index); } this.BaseAdd(index, value); } } public new PokerClient this[string responseString] { get { return (PokerClient)BaseGet(responseString); } set { if (BaseGet(responseString) != null) { BaseRemoveAt(BaseIndexOf(BaseGet(responseString))); } BaseAdd(value); } } protected override System.Configuration.ConfigurationElement CreateNewElement() { return new PokerClient(); } protected override object GetElementKey(System.Configuration.ConfigurationElement element) { return ((PokerClient)element).Name; } } public class PokerClientsConfig : ConfigurationSection { public static PokerClientsConfig GetConfig() { return (PokerClientsConfig)ConfigurationManager.GetSection("PokerClients") ?? new PokerClientsConfig(); } [System.Configuration.ConfigurationProperty("PokerClients")] [ConfigurationCollection(typeof(PokerClients), AddItemName = "PokerClient")] public PokerClients PokerClients { get { object o = this["PokerClients"]; return o as PokerClients; } } } }
when trying to get a section (method GetConfig)
var Config = PokerClientsConfig.GetConfig(); an error occurs:
Raw exception of type "System.Configuration.ConfigurationErrorsException" in System.Configuration.dll
Additional information: Error while creating a configuration section handler for PokerClients: Failed to load type "WindowsFormsApplication1.PokerClientsConfig" from the assembly "System.Configuration, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a"
What do I need to write in the type property? I understood that [namespace] is being written there. [Class handler] and then in most cases the same namespace (I tried it too), but also “I cannot load a file or assembly”.