Skip to content
AppSettings class mapper
C# Batchfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Examples
Jupiter1.AppSettingsMapper.Tests
Jupiter1.AppSettingsMapper
.gitignore
AppSettingsMapper.sln
Readme.md

Readme.md

AppSettingsMapper library v.0.3.1

The AppSettingsMapper library used for map App.config appSettings section items to custom C# class. Library allows to map all property types such as int, short, double, TimeSpan, DateTime etc. Also you can map on custom property types of C# class.

Example:

For example you have App.config file like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="SqlExpressConnection" connectionString="Data Source=localhost\SqlExpress; Initial catalog=Test;" />
  </connectionStrings>
  <appSettings>
    <add key="ShowTooltip" value="true" />
    <add key="TemplatesFolder" value="Templates/" />
    <add key="LockDuration" value="1:30:00" />
    <add key="IsCompiled" value="true" />
    <add key="RequiredKey" value="123" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

and you have a settings class AttributesExampleSettings.cs:

class AttributesExampleSettings
{
	public ConnectionStringSettings SqlExpressConnection { get; set; }

	public bool ShowTooltip { get; set; }

	public string TemplatesFolder { get; set; }

	public TimeSpan LockDuration { get; set; }
	
	[Key("IsCompiled")]
	public bool A { get; set; }

	[Required(typeof(FileNotFoundException))]
	public int RequiredKey { get; set; }

	[Default("admin")]
	public string User { get; set; }

	[Ignore]
	public int IgnoredValue { get; set; }

	public AttributesExampleSettings()
	{
		IgnoredValue = 1234;
	}
}

than you can use AppSettingsMapper for map appSettings section to AttributesExampleSettings class:

var settings = AppSettings.MapTo<AttributesExampleSettings>();

// settings.SqlExpressConnection.Name = "SqlExpressConnection"
// settings.SqlExpressConnection.ConnectionString = "Data Source=localhost\SqlExpress; Initial catalog=Test;"
// settings.ShowTooltip = true;
// settings.TemplatesFolder = "Templates/";
// settings.LockDuration = new TimeStamp(1, 30, 0);
// settings.A = true; // Value from IsCompiled setting.
// settings.RequiredKey = 123;
// settings.User = "admin";
// settings.IgnoredValue = 1234;

To install:

Run the following command in the Package Manager Console to install:

Install-Package AppSettingsMapper

Contacts:

Skype: dimaermakov

E-mail: elwray.ru@gmail.com

You can’t perform that action at this time.