0

I have this code here where I am trying to save each field in an instance of a class to a separate file. I have it all written out, but something just does not look right about it. The problem is that it doesn't reference the instance, which contains the data, just the type. And that to me doesn't quite seem right. I am using System.Reflection. Now, how do I reference the instance? Or am I already and don't know it. Here is my code:

public static void Save(appData data)
{
    string filename;
    // this does not accept the variable "data", only the class spec "appData"
    var fields = typeof(appData).GetFields(BindingFlags.Instance);
    foreach (FieldInfo field in fields)
    {
        try
        {
            filename = (string)field.GetValue("dataFile");
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
            Console.ReadLine();
        }
        dataStream = new FileStream(filename,
            FileMode.Truncate, FileAccess.Write,
            FileShare.Read);
        serial.Serialize(dataStream, field );
        dataStream.Flush();
        dataStream.Close();
        dataStream = null;
    }

}
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
  • Your code looks incomplete: `serial.Serialize(dataStream, );` is not valid. – Jason Allen Jul 15 '12 at 01:47
  • There, fixed. Does that make it anymore clear? :) – Arlen Beiler Jul 15 '12 at 01:53
  • `serial` does not look like it is defined either – Jason Allen Jul 15 '12 at 01:54
  • Can you please show the definition of the type `appData`? It looks like you are trying to get the value of the "dataFile" member of the appData instance. Is it a private field which prevents you from accessing it directly? – Monroe Thomas Jul 15 '12 at 01:57
  • No, the datafile variable is a field that each field in appData contains and is not serialized. It is assigned on initialization of appData or deserialization of the field. It is simply the file path of the datafile for that field. Yes, it is public. Also, serial is a binary formatter. – Arlen Beiler Jul 15 '12 at 05:03
  • What I was trying to do was something similar to method.invoke. I guess I just need to call getfield and getvalue a second time, and provide it with the field.getvalue(data). – Arlen Beiler Jul 15 '12 at 05:32

2 Answers2

2

Although your code is incomplete, I think I can tell what you're trying to do.

Your example is actually serializing the FieldInfo object, not the fields. You want to replace:

filename = (string)field.GetValue("dataFile");

with:

filename = field.Name;

Also replace:

serial.Serialize(dataStream, field );

with:

serial.Serialize(dataStream, field.GetValue(data);

Just make sure that all of your fields are able to be serialized by your serial object.

Michael Graczyk
  • 4,905
  • 2
  • 22
  • 34
0

You are not calling the FieldInfo.GetValue method correctly. Its argument should be the object whose field value you want to extract, or null for static fields.

Maybe what you were trying to achieve by this:

filename = (string)field.GetValue("dataFile");

Was this (assuming data is the instance of interest)?

if (field.Name == "dataFile")
{
    filename = (string)field.GetValue(data);
}
Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44
  • In that case he should have called the [FieldInfo.Name property](http://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.name.aspx) instead of the GetValue method. – Thomas C. G. de Vilhena Jul 15 '12 at 02:12