Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

serialization - C#, can't serialize to binary

I follow this tutorial online exactly but somehow it's giving me errors. Saying there is no object map or something.

I have the following static object that I want to serialize:

[Serializable]
public class Settings : ISerializable 
{
    public static string server= "http://localhost/";
    public static string username = "myname";
    public static bool savePassword = true;
    public static bool autoSync = true;
    public static string password = "mypass";
    public static string folderPath1= "c:/";
    public static string folderPath2= "c:/";
    public static string autoSyncDuration = "300";
    public static string lastSyncTime = "???";


    public Settings()
    { }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Type myTypeObj = Type.GetType("Settings");
        foreach (FieldInfo p in myTypeObj.GetFields())
        {
            Object value = p.GetValue(null);
            info.AddValue(p.Name, value, p.GetType());
        }
    }

    public Settings(SerializationInfo info, StreamingContext context)
    {
        Type myTypeObj = Type.GetType("Settings");
        FieldInfo p;
        foreach (SerializationEntry e in info)
        {
            p = myTypeObj.GetField(e.Name);
            p.SetValue(null, e.Value);
        }
    }
}

And here is the Read/Write functions:

    private void writeSettings()
    {
        pcb_savingSettings.Visible = true;
        FileStream fileStream = new FileStream(settingFile, FileMode.Create, FileAccess.Write, FileShare.None);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fileStream, new Settings());

        fileStream.Close();
        pcb_savingSettings.Visible = false;
    }
    private void readSettings()
    {
        if (!File.Exists(settingFile))
        {
            writeSettings();
        }
        FileStream fileStream = new FileStream(settingFile, FileMode.Open, FileAccess.Read, FileShare.None);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Deserialize(fileStream);
        fileStream.Close();
    }

ACTUAL ERROR MSG: No map for Object '822476800'. This occur on this line:

bf.Deserialize(fileStream);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'll preface this answer with this is a bad idea. Serialization is designed to serialize an object instance and static fields are not part of that instance.

I believe that when you do have a custom serializer, you need to preface the object name with static.. For example, public static member named A would need added as static.A.

Here's a link that should help: http://forums.codeguru.com/showthread.php?t=411604


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...