DefaultAttribute

If value is not found in configuration file, value from DefaultAttribute will be used.

Usage

[Config]
public class Config
{
  [Default("default")]
  public static string FieldwithDefaultValue { get; set; }
}

Min Max constraints

If you have some values in configuration that should be greater or lesser than certain value, you can use min and max attributes.

Usage

public class Config
{
  [Min(0), Max(23)]
  public static string Hour{ get; set; }

  [Min(0), Max(59)]
  public static string Minute{ get; set; }
}

RequiredAttribute

Indicates that value should be set. Configuration initializer will throw an exception if field is not set in configuration file.

Usage

public class Config
{
  [Required]
  public static string SomeRequiredField{ get; set; }
}

PathAttribute

When PathAttribute is set, configuration initializer will ensure that path is rooted. Also it ensures that directory exists.

Usage

public class Config
{
  [Path]
  public static string PathThatWillBeCreatedIfItDoesNotExist{ get; set; }

  [Path(false)]
  public static string SomePathThatShouldExist{ get; set; }

  [Path)]
  public static string AlreadyRootedPath{ get; set; }
}

Config file
<appSettings>
  <add key="PathThatWillBeCreatedIfItDoesNotExist" value="path1" />
  <add key="SomePathThatShouldExist" value="path2" />
  <add key="AlreadyRootedPath" value="C:/temp" />
  <add key="AlreadyRootedPath2" value="/rootedPath" />
</appSettings>

Last edited Feb 6, 2013 at 9:29 AM by maxlego, version 5

Comments

No comments yet.