aidin jj
aidin jj

Reputation: 43

how to serialize and deserialize over two different assembly?

This is a C# project. I am using BinaryFormatter as the serializer and deserializer for my applications.
Because there are two application and the assembly names are different from each other, I am using a serialization binder to solve the issue on finding types.
but I still get an error which is :

Unable to find assembly 'GameServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

and this is my serialization binder code :

    public override Type BindToType(string assemblyName, string typeName)
    {
        if (assemblyName.Contains("GameServer"))
        {
            assemblyName = "Assembly-CSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12345";
        }
        return Type.GetType(typeName);
    }

in the above code, i just check if an assembly named "GameServer" found, then rename it to the local assembly name.
am i missing something here?

Upvotes: 1

Views: 569

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062502

Honestly, the only "good" answer here is "don't use BinaryFormatter", followed closely by "really, don't use BinaryFormatter". There are many good reasons that it is considered deprecated in .NET Core / .NET 5+, and this minor inconvenience of matching similar types: didn't even make that list! (Btw, you can avoid having to do that by moving your DTO types to a library assembly that both other projects reference).

Note: for virtually any other serializer - JSON, xml, protobuf, etc - it would already "just work": the types being in different assemblies wouldn't remotely matter. So frankly, the best advice here is simply: "change serializer".

Upvotes: 3

Related Questions