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

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:

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!

for more information on singletons, see:

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

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.