|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
ConfigurationProperty, classe
Cette API prend en charge l'infrastructure .NET Framework et n'est pas destinée à être utilisée directement à partir de votre code.
Espace de noms : System.Configuration
Assembly : System.Configuration (dans System.Configuration.dll)
Le type ConfigurationProperty expose les membres suivants.
| Nom | Description | |
|---|---|---|
![]() | ConfigurationProperty(String, Type) | |
![]() | ConfigurationProperty(String, Type, Object) | |
![]() | ConfigurationProperty(String, Type, Object, ConfigurationPropertyOptions) | |
![]() | ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions) | |
![]() | ConfigurationProperty(String, Type, Object, TypeConverter, ConfigurationValidatorBase, ConfigurationPropertyOptions, String) |
| Nom | Description | |
|---|---|---|
![]() | Converter | |
![]() | DefaultValue | |
![]() | Description | |
![]() | IsAssemblyStringTransformationRequired | |
![]() | IsDefaultCollection | |
![]() | IsKey | |
![]() | IsRequired | |
![]() | IsTypeStringTransformationRequired | |
![]() | IsVersionCheckRequired | |
![]() | Name | |
![]() | Type | |
![]() | Validator |
| Nom | Description | |
|---|---|---|
![]() | Equals(Object) | |
![]() | GetHashCode | |
![]() | GetType | |
![]() | ToString |
Modèle de programmation. Ce modèle nécessite la création d'une propriété pour chaque attribut d'élément afin d'obtenir et/ou définir sa valeur et de l'ajouter au sac de propriétés interne de la classe de base ConfigurationElement sous-jacente. Modèle déclaratif. Ce modèle plus simple, également appelé modèle attribué, vous permet de définir un attribut d'élément au moyen d'une propriété et de le décorer avec des attributs. Ces attributs donnent au système de configuration ASP.NET des informations sur les types de propriété et leurs valeurs par défaut. Avec ces informations, obtenues par réflexion, le système de configuration ASP.NET crée pour vous les objets de propriétés d'éléments et exécute l'initialisation requise.
L'exemple de code suivant montre comment utiliser ConfigurationProperty lorsque vous créez une section personnalisée.
using System; using System.Configuration; using System.Collections; using System.ComponentModel; namespace ConfigurationPropertyExample { // Define a custom section. // Shows how to use the ConfigurationProperty // class when defining a custom section. public sealed class CustomSection : ConfigurationSection { // The collection (property bag) that contains // the section properties. private static ConfigurationPropertyCollection _Properties; // The FileName property. private static ConfigurationProperty _FileName; // The Alias property. private static ConfigurationProperty _Alias; // The MaxUsers property. private static ConfigurationProperty _MaxUsers; // The MaxIdleTime property. private static ConfigurationProperty _MaxIdleTime; // CustomSection constructor. static CustomSection() { // Initialize the _FileName property _FileName = new ConfigurationProperty("fileName", typeof(string), "default.txt"); // Initialize the _MaxUsers property _MaxUsers = new ConfigurationProperty("maxUsers", typeof(long), (long)1000, ConfigurationPropertyOptions.None); // Initialize the _MaxIdleTime property TimeSpan minTime = TimeSpan.FromSeconds(30); TimeSpan maxTime = TimeSpan.FromMinutes(5); ConfigurationValidatorBase _TimeSpanValidator = new TimeSpanValidator(minTime, maxTime, false); _MaxIdleTime = new ConfigurationProperty("maxIdleTime", typeof(TimeSpan), TimeSpan.FromMinutes(5), TypeDescriptor.GetConverter(typeof(TimeSpan)), _TimeSpanValidator, ConfigurationPropertyOptions.IsRequired, "[Description:This is the max idle time.]"); // Initialize the _Alias property _Alias = new ConfigurationProperty("alias", typeof(string), "alias.txt"); // Initialize the Property collection. _Properties = new ConfigurationPropertyCollection(); _Properties.Add(_FileName); _Properties.Add(_Alias); _Properties.Add(_MaxUsers); _Properties.Add(_MaxIdleTime); } // Return the initialized property bag // for the configuration element. protected override ConfigurationPropertyCollection Properties { get { return _Properties; } } // Clear the property. public void ClearCollection() { Properties.Clear(); } // Remove an element from the property collection. public void RemoveCollectionElement(string elName) { Properties.Remove(elName); } // Get the property collection enumerator. public IEnumerator GetCollectionEnumerator() { return (Properties.GetEnumerator()); } [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public string FileName { get { return (string)this["fileName"]; } set { this["fileName"] = value; } } [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] public string Alias { get { return (string)this["alias"]; } set { this["alias"] = value; } } [LongValidator(MinValue = 1, MaxValue = 1000000, ExcludeRange = false)] public long MaxUsers { get { return (long)this["maxUsers"]; } set { this["maxUsers"] = value; } } public TimeSpan MaxIdleTime { get { return (TimeSpan)this["maxIdleTime"]; } set { this["maxIdleTime"] = value; } } } }
<configuration>
<configSections>
<section name="CustomSection" type="ConfigurationPropertyExample.CustomSection, ConfigurationPropertyExample"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="override.txt" alias="alias.txt"
maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>
// Define a custom section programmatically. static void CreateSection() { try { CustomSection customSection; // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Create the section entry // in the <configSections> and the // related target section in <configuration>. // Call it "CustomSection2" since the file in this // example already has "CustomSection". if (config.Sections["CustomSection"] == null) { customSection = new CustomSection(); config.Sections.Add("CustomSection2", customSection); customSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException err) { Console.WriteLine(err.ToString()); } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
