Class ConfigurationAttribute
An attribute that can be used to specify the default value to return if no value is defined in the configuration root.
Inheritance
Namespace: OpenCollar.Extensions.Configuration
Assembly: OpenCollar.Extensions.Configuration.dll
Syntax
[AttributeUsage(AttributeTargets.Property)]
public sealed class ConfigurationAttribute : Attribute
Remarks
Use the DefaultValue property of this attribute to manage the default value for configuration items that do not require and underlying setting.
The Persistence property of this attribute can be used to control whether values are read or written to and from the underlying configuration source.
The following UML has been generated directly from the source code using Jebbs PlantUML .
Examples
In the example below a property on an interface is defined with a default value, meaning that a value will be set, and no error will be thrown, even if there is no value in the underlying configuration service. In this case the result is a property that will be set to "DEFAULT" if no name is specified in configuration file.
[Configuration(DefaultValue = "DEFAULT")]
string Name
{
get; set;
}
In the following example the attribute is used to define the way in which a property is persisted. The value will be loaded from the configuration file, but never saved back to the file. This allows the application to change the value for all consumers of the interface, but it will never be changed in the file.
[Configuration(Persistence = ConfigurationPersistenceActions.LoadOnly)]
string InitialValue
{
get; set;
}
Constructors
| Improve this Doc View SourceConfigurationAttribute()
Initializes a new instance of the ConfigurationAttribute class.
Declaration
public ConfigurationAttribute()
Properties
| Improve this Doc View SourceDefaultValue
Gets the default value.
Declaration
public object DefaultValue { get; set; }
Property Value
Type | Description |
---|---|
System.Object | The default value to return if no value is defined in the configuration root. Can be null. |
Persistence
Gets or sets the policy for when to load and save values from the configuration service.
Declaration
public ConfigurationPersistenceActions Persistence { get; set; }
Property Value
Type | Description |
---|---|
ConfigurationPersistenceActions | The policy for when to load and save values from the configuration service. |