TOC

The community is working on translating this tutorial into Greek, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Reflection:

A Reflection based settings class

Okay, so I thought that I would end this part of the tutorial about Reflection, with a cool and useful example. It's a bit bigger than the usual examples here at the site, but hopefully you will find it really useful. It uses a bunch of the stuff that we have looked into during the last couple of chapters, so hopefully you can keep up.

A common scenario when creating any sort of application, is the desire to save the users settings. When you get several settings, you will probably create a Settings class, which will handle loading and saving of the desired settings. Each time you need a new setting in your Settings class, you will have to update the Load() and Save() methods, to include this new setting. But hey, why not let the Settings class discover its own properties and then load and save them automatically? With Reflection, it's quite easy, and if you have read the other chapters in the Reflection section of this tutorial, you should be able to understand the following example.

To make it fit better into a small example, I am saving information about a person instead of application settings, but hopefully you will get the general idea anyway. Please be aware that using Reflection WILL be slower than reading and writing known properties manually, so you should consider when to use it and when to opt for a faster approach! Also, in our example, we use a simple text file for storing even simpler values, only separated by a | (pipe character). If you're using this for real world stuff, you will probably want a better format for your data, perhaps XML. And of course, there is not much error handling, so you should probably add some of that as well.

Okay, let's get started. First, our Person class, which you can simply rename to Settings or something like that, to make it more useful to you:

public class Person
{
    private int age = -1;
    private string name = String.Empty;

    public void Load()
    {
        if(File.Exists("settings.dat"))
        {
            Type type = this.GetType();

            string propertyName, value;
            string[] temp;
            char[] splitChars = new char[] { '|' };
            PropertyInfo propertyInfo;

            string[] settings = File.ReadAllLines("settings.dat");
            foreach(string s in settings)
            {
                temp = s.Split(splitChars);
                if(temp.Length == 2)
                {
                    propertyName = temp[0];
                    value = temp[1];
                    propertyInfo = type.GetProperty(propertyName);
                    if(propertyInfo != null)
                        this.SetProperty(propertyInfo, value);
                }
            }
        }
    }

    public void Save()
    {
        Type type = this.GetType();
        PropertyInfo[] properties = type.GetProperties();
        TextWriter tw = new StreamWriter("settings.dat");
        foreach(PropertyInfo propertyInfo in properties)
        {
            tw.WriteLine(propertyInfo.Name + "|" + propertyInfo.GetValue(this, null));
        }
        tw.Close();
    }

    public void SetProperty(PropertyInfo propertyInfo, object value)
    {
        switch(propertyInfo.PropertyType.Name)
        {
            case "Int32":
                propertyInfo.SetValue(this, Convert.ToInt32(value), null);
                break;
            case "String":
                propertyInfo.SetValue(this, value.ToString(), null);
                break;
        }
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}

Okay, there's a lot of stuff, I know. But I will help you through the entire class. First of all, we have a couple of private fields, for holding information about our person. In the bottom of the class, we have the corresponding public properties which uses the private fields of course.

We also have a Load() method. It looks for the file settings.dat, and if it exists, it reads the entire file and places each line of it in an array of strings. Now, we run through each setting line, and splits it up into two parts: A property name and a value part. If both is present, we simply use the Type object to get the property with the property name, and then we set the value for it, using our own SetProperty method.

The SetProperty() method looks at the type of the property about to be changed, and then acts correspondingly. Right now, it only supports integers and strings, but as you probably realize, extending this support would be quite easy.

The Save() method gets an array of PropertyInfo instances, one for each of the defined properties on the Person class, and then uses a TextWriter to write each property, and its value, to the data file.

Now we just need some code to use this class. This small application will try to load the person from the settings file, and if it doesn't succeed, the user is prompted for the information:

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.Load();
        if((person.Age > 0) && (person.Name != String.Empty))
        {
            Console.WriteLine("Hi " + person.Name + " - you are " + person.Age + " years old!");
        }
        else
        {
            Console.WriteLine("I don't seem to know much about you. Please enter the following information:");
            Type type = typeof(Person);
            PropertyInfo[] properties = type.GetProperties();
            foreach(PropertyInfo propertyInfo in properties)
            {
                Console.WriteLine(propertyInfo.Name + ":");
                person.SetProperty(propertyInfo, Console.ReadLine());
            }
            person.Save();
            Console.WriteLine("Thank you! I have saved your information for next time.");
        }
        Console.ReadKey();
    }
}

Everything here is pretty trivial, except for the part where we ask the user for information. Once again, we use Reflection, to get all the public properties of the Person class, and then ask for each of them.

As a reader exercise, I suggest that you extend the Person class to include more information. Simply add more properties to it, and you will see that this information gets saved and loaded too.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!