5

For a game I'm developing on (in C#), we need to be able to save and load options files, and for ease of use, we decided to do it as plain text. I have a problem however when I try to load the text back into their variables as I don't always know what type of variable it needs to be loaded into.

The following line of code works, except for the fact that I have yet to find functionallity that resembles

f.GetType().Parse()

Heres the actual code

OptionsClass current = new OptionsClass();  
    using(StreamReader reader = new StreamReader(path)){
        string line;
        while((line = reader.ReadLine()) != null){
        foreach(FieldInfo f in typeof(OptionsClass).GetFields()){
            f.SetValue(current, f.GetType().Parse(line.Split(new char[] {'='})[1]));

        }
    }
}

Let me know if anything is unclear, or more information is needed. Regards, -Logan

2
  • 3
    What you're trying to do is known as serialization. If you search for ".net serialization", there is a wealth of info on how to persist and restore information automatically based on the structure/contents of a class. Commented Nov 30, 2011 at 23:51
  • If you use something like SOAP, it serializes all the datatypes along with the values. It's designed for doing exactly this. Commented Nov 30, 2011 at 23:56

5 Answers 5

6

Rather than trying to do this yourself, I would suggest that you use the builtin XML or JSON serialiation.

However, if you are going to implement this yourself, then I would suggest that you perform a switch on the type of field and convert the value according to the data type of the field. For example:

        string sValue = line.Split(new char[] {'='})[1]);
        object oValue;
        switch (f.FieldType.Name.ToLower())
        {
            case "system.string":
               oValue = sValue;
               break;

            case "system.int32":
                oValue = Convert.ToInt32(sValue);
                break;
         etc...

      f.SetValue(current, oValue);

One additional note: if you are going the self-built route, then you probably want to be a little more robust in the data conversions (i.e. checking to see if the string is not null and is numeric when converting to a number, etc).

Sign up to request clarification or add additional context in comments.

Comments

4

I'd definitely add that this is a perfect case for serialization.

However, if you want, you can also use:

Convert.ChangeType(object value, Type type)

So in the example above it would be something like:

f.SetValue(current, Convert.ChangeType(f.GetType().Parse(line.Split(new char[] {'='})[1])), f.GetType());

Note this doesn't check for null values.

Comments

3

Are you looking for something like this? Assuming you know what order your objects are coming in:

var converter = TypeDescriptor.GetConverter(f.GetType());
var result = converter.ConvertFrom(line.Split(new char[] {'='})[1]));

Comments

0

If you wrote it out, you should know what order you wrote it out in, and read it in the same order.

1 Comment

True, it's just that my options class contains quite a few variables, and as we are still developing i may add more later, so i just want to limit the amount of places i have to make changes when/if i do make changes.
0

go to your Project Properties -> Settings -> Create the settings you want,

then to get at them

var x = Properties.Settings.Default.MyCoolSetting;

(you can read and write them)

these settings will be stored in your applications .config file under user settings, where you can edit them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.