Sto riscontrando questo strano problema ... nel mio codice se imposto il valore di IsRequired su false o true allora rimane falso .. Tuttavia se inserisco un valore predefinito funziona?C# - ConfigurationSection isRequired Attributo
Il codice non-lavoro è:
public class FtpSettingsSection : ConfigurationSection
{
[ConfigurationProperty("host", IsRequired = true)]
public HostElement Host
{
get { return (HostElement)this["host"]; }
set { this["host"] = value; }
}
}
public class HostElement : ConfigurationElement
{
[ConfigurationProperty("URL", IsRequired = true)]
public string URL
{
get { return (string)this["URL"]; }
set { this["URL"] = value; }
}
}
e il codice di lavoro è:
public class FtpSettingsSection : ConfigurationSection
{
[ConfigurationProperty("host", DefaultValue = "", IsRequired = true)]
public HostElement Host
{
get { return (HostElement)this["host"]; }
set { this["host"] = value; }
}
}
public class HostElement : ConfigurationElement
{
[ConfigurationProperty("URL", DefaultValue = "", IsRequired = true)]
public string URL
{
get { return (string)this["URL"]; }
set { this["URL"] = value; }
}
}
Come mai che ho bisogno di impostare DefaultValue ""?
Il primo esempio di codice dosent genera eccezioni anche se la proprietà host non è definita nel file di configurazione? – ebb
Il primo esempio genererà un'eccezione, la seconda con l'attributo predefinito no. – dexter
In realtà ho provato questo e il primo esempio non genera un'eccezione. In un'app console di prova IsRequired sembra essere ignorata. Se è richiesto non è necessario inserire il valore predefinito perché in realtà non è richiesto. Se lo contrassegni, IsRequired = true, allora dovrebbe generare un'eccezione se non fornisci un valore predefinito, ma non lo fa. Ho provato in .Net 4. –