Fill a dictionary with properties of a class

There are 7 private functions

Function: ConfigurationExists()

The ConfigurationExists function, just checks if the file exists or not

Function: CreateConfiguration()

Within the Function it is first checked if the Configuration has been set, if not the Function “SetDefaultValues()” creates a set of standard values for the properties. Next we check if the Configuration exists, this is done because saving the normal configuration and creating a standard configuration is done through the same function.

We iterate through the dictionary and write all the key-value Items into the configuration file.

Function: SetDefaultValues()

This function just creates a set of default values, if no file has been found.

Function: ReadConfiguration()

The ReadConfiguration function opens a Filestream to the configuration file and reads all key-value pairs, line by line, every line is split and given to the “UpdateDictionaryValue”-Function.

After all Dictionary Entries are updated, the Dictionary is transfered to the Singleton object, so it can be accessed by other parts of the program.

Function: UpdateDictionaryValue(configItem)

The Function checks if an configuration item is within the dictionary and if it finds it, updates the value for this particular item.

Now to the last 2 private functions were all the magic happens, actually the refer to a new class, but we go there in just a minute.

Funcion: PropertiesToDictionary()

This function takes itself (this) and gets a Dictionary via a static class and reflections with all the properties as key-value pairs

Function: DictionaryToProperties()

This function transfers all the Key-Value Pairs of the dictionary to the singleton for external excess.

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.