0

I'm trying to make a save system for my project. I want binary serialization, and I'm trying to use BuinaryFormatter. I Have no error when performing save, but if I call load, I get this error:

SerializationException: Type 'TheCompany.SaveSystem.ISaveData' in Assembly 'TheCompany.SaveSystem, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Load and save code looks so:

        public void Save()
        {
            GameSave gameSave = new GameSave(GetSaveData());
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
            binaryFormatter.Serialize(file, gameSave);
            file.Close();
        }

        public GameSave Load()
        {
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/gamesave.save",        FileMode.Open);
            GameSave gameSave = (GameSave)binaryFormatter.Deserialize(file);
            file.Close();
            return gameSave;
        }

Data class:

    [Serializable]
    public class GameSave 
    {
        private Dictionary<Type, HashSet<ISaveData>> _savedata;

        public Dictionary<Type, HashSet<ISaveData>> Savedata => _savedata;

        public GameSave(Dictionary<Type, HashSet<ISaveData>> savedata)
        {
            _savedata = savedata;
        }
    }

ISaveData:

    public interface ISaveData : ISerializable
    {

    }

I'm sure all of ISaveData implementations are marked with [Serializable] Other way, an error would occurs on save. In my tests _savedata contains only one key with Hashset of one element. When I'm trying to save only this one ISaveData in structure like this, its works completely fine.

    [Serializable]
    public class GameSave 
    {
        public ISaveData Savedata;
    }

Update: All my concrete implementations of ISaveData are structs. When I changed them into classes, the exception disappeared.

dreadi
  • 11
  • 1
  • 1
    The error says ISaveData is not serialized, its the one thing you have not shared – BugFinder Sep 02 '23 at 09:56
  • But if I'm trying to save only one field of type ISaveData It works. – dreadi Sep 02 '23 at 10:17
  • ISaveData looks so: `public interface ISaveData : ISerializable { }` – dreadi Sep 02 '23 at 10:29
  • This is a [known](https://github.com/koszeggy/KGySoft.CoreLibraries/blob/50ab484c8d49f26ba477da8abf6b0ded62f9830a/KGySoft.CoreLibraries.UnitTest/UnitTests/Serialization/Binary/BinarySerializerTest.Tests.cs#L1671C6) limitation of `BinaryFormatter` if interfaces are used in generics. Btw, `BinaryFormatter` is just getting obsoleted due to its [security issues](https://stackoverflow.com/a/67107584/5114784), and `Type` is no longer serializable on newer platforms (.NET/.NET Core). – György Kőszeg Sep 02 '23 at 18:23
  • As an alternative, you can try [my serializer](https://github.com/koszeggy/KGySoft.CoreLibraries#binary-serialization), which is more secure and also [produces](https://dotnetfiddle.net/nQfFrQ) much more compact results. Functionally it is compatible with `BinaryFormatter` and it supports serializing `Type` not just on .NET Framework but on every platform. – György Kőszeg Sep 02 '23 at 18:25
  • Thanks. I will check this solution. Is there the same problem with interfaces in generics or can it handle these cases? – dreadi Sep 02 '23 at 20:40

0 Answers0