Fill a dictionary with properties of a class

Following scenario… you need to create a key/value configuration file within a configuration class. Properties are the Keys and their value is the value ;). Values for the properties need to be assigned automaticly when the “configuration file” is loaded, in a way that the class can be extended with new properties and no new code needs to be added.

One of the solutions I found, was to use a Singleton-Pattern with

Dictionary<string, object>

that holds all the property keys and values.

Lets walk this through with a real life example. Nlog is used for logging. At the End you can see the whole class, but first let us go through everything step by step.

First the properties:

public bool ConfigurationSet { get; set; }
public string RemoteIPAddress { get; set; }
public string RemotePort { get; set; }
public string LocalIPAddress { get; set; }
public string LocalPort { get; set; }
public bool TCP { get; set; }
public bool UDP { get; set; }
public bool Multicast { get; set; }
public bool GzipCompression { get; set; }
public string ReceiveBufferSize { get; set; }
public string MaxSegmentSize { get; set; }
public string SocketBufferSize { get; set; }
public string DateMatchGroup { get; set; }
public string TextAfterDateMatch { get; set; }
public string TextBeforeProviderMatchGroup { get; set; }
public string ProviderMatchGroup { get; set; }
public string TextBeforeXMLCallListMatchGroup { get; set; }
public string FullRegex { get; private set; }
public string CallListMatchGroup { get; set; }
public string ConfigurationPath
{
	get
	{
		return _configurationPath;
	}
	private set => _configurationPath = value;
}
public string ConfigurationFile
{
	get
	{
		return _configurationFile;
	}
	private set => _configurationFile = value;
}
public string ExtractionFileName { get; set; }
public string ExtractionFolder { get; set; }
public string MulticastSendTimeoutInMS { get; set; }

The “ConfigurationSet” Property is not used in the configuration file. It can be used for other parts of the program to find out if the configuration has been loaded and the properties have values.

Then there is the Dictionary as <string, object> and the singleton with the private and static constructor, this is thread-safe!

private static readonly object _locker = new object();
public Dictionary<string, object> ConfigurationItems { get; private set; }
private static readonly ConfigurationHelper _configuration = new ConfigurationHelper();
public static ConfigurationHelper Configuration
{
	get
	{
		return _configuration;
	}
	private set
	{
		lock(_locker)
		{
			_configuration = value;
			Monitor.Pulse(_locker);
		}
	}
}

private ConfigurationHelper() { }

static ConfigurationHelper() { }

for more information on singletons, see: http://csharpindepth.com/Articles/General/Singleton.aspx

The last Members are the Configurationfolder, the configurationfilename and the logger (nlog).

private string _configurationPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\Reflection\";
private string _configurationFile = "reflection.config";
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
This entry was posted in C#, PC, programming, Sonstiges, Wissen, Work and tagged , , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.