C# BinaryFormatter未序列化字段

C# BinaryFormatter未序列化字段,c#,serialization,xna-4.0,C#,Serialization,Xna 4.0,我在让BinaryFormatter实际序列化这个字段时遇到了一些问题 我有一个Tile类,序列化它的位置和ID,然后使用BinaryFormatter保存所有这些Tile。其他每个字段都是非序列化的,并使用ID的值进行初始化 但是,ID字段不会序列化,其他所有内容都会序列化。这可能是什么原因 这是我的平铺字段: // The ID of this tile private int id; // The name visible for the tile in t

我在让BinaryFormatter实际序列化这个字段时遇到了一些问题

我有一个Tile类,序列化它的位置和ID,然后使用BinaryFormatter保存所有这些Tile。其他每个字段都是非序列化的,并使用ID的值进行初始化

但是,ID字段不会序列化,其他所有内容都会序列化。这可能是什么原因

这是我的平铺字段:

        // The ID of this tile
    private int id;
    // The name visible for the tile in the map editor
    [NonSerialized] private String name;
    // The file location for this tile for loading a texture
    [NonSerialized] private String fileName;

    // Sprite for drawing this tile
    [NonSerialized] private Sprite sprite;
    private Transform transform = new Transform();

    // If this tile is blocked, blocked tiles can not be walked over are classes as "unpassable"
    [NonSerialized] private bool blocked;
    // Used to determine if a specific tower types can be placed on this tile, if any
    [NonSerialized] private short towerMask;
    // The tower that is placed on this tile, if any
    [NonSerialized] private Tower tower;
    // A path generated for enemies to navigate from this tile to another, used for tiles that can spawn enemies
    [NonSerialized] private Path path;
加载地图时,我会执行以下操作:

            map = MapSerializer.Load("test");
MapSerializer类只是通常的设置,具有静态加载和保存方法,如下所示:

        public static Map Load(string filename){
        Map map;
        String path = Path.Combine("maps/", filename+".mp");
        BinaryFormatter formatter = new BinaryFormatter();

        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read);

        try{
            Trace.WriteLine("Loading map from " + path, "Map Serialization");
            map =  (Map) formatter.Deserialize(stream);
            Trace.WriteLine("Map Loaded!", "Map Serialization");
        }
        finally{
            stream.Close();
        }

        return map;
    }
另一个恼人的问题是,当加载tile时,它不会调用no args构造函数。这是怎么回事?我必须手动循环所有的tile并调用Create,尽管它是在no-args构造函数中调用的

有没有发现一个int字段没有写入光盘

编辑:

序列化代码:

 public static void Save(Map data, String filename){
        String path = Path.Combine("maps/", filename + ".mp");
        BinaryFormatter formatter = new BinaryFormatter();

        if (!Directory.Exists("maps"))
            Directory.CreateDirectory("maps");
        FileStream stream = File.Open(path, FileMode.Create);

        try{
            Trace.WriteLine("Writing map to " + path, "Map Serialization");
            formatter.Serialize(stream, data);
            Trace.WriteLine("Map Saved!", "Map Serialization");
        }
        catch (SerializationException e){
            Trace.WriteLine("Failed to save map : Error - " + e.Message);
            throw;
        }
        finally{
            // Close the file
            stream.Close();
        }
    }

您尚未发布序列化代码。关于你的第二期

序列化将不会调用默认构造函数。如果实现ISerializable,则将调用采用序列化特定参数的构造函数

另一种方法是实现System.Runtime.Serialization.IDeserializationCallback。有关详细信息,请参见此处


另一种方法是用属性修饰一个方法。

可以包含序列化代码吗?我已经更新了问题,看起来还可以。如何初始化Tile类中的ID字段?有两种方法。在map editor中,我通过调用获取ID的构造函数来创建所有模板平铺,然后它使用该ID为类的其余部分选择正确的参数。第二种方法是,当实际在地图上放置/更改磁贴时,我只需执行Tile Tile=template.Clone,它将返回一个具有相同ID的新磁贴。我在处理磁贴创建的代码的每个部分都签入了,它们当时都使用了正确的ID。如果不是,它们会有各种各样的混乱参数,比如纹理;这反映了瓷砖的类型很好。我看不出有什么问题。事实上,当我在平铺上运行二进制格式化程序序列化时,它是有效的。查看我在网站上使用的代码,您将无法在线运行它。。我只是在分享代码是的,我在发布后不久发现了IDeserializationCallback,这是一个已修复的问题。