Skip to content

SerializationBinder.BindToName not called for an Array Type #108444

Open
@LievaartR

Description

@LievaartR

Description

I know I should not be using this. But for now I am stuck with it.

In this case I am working on backwards compatibility of data files with older versions of software. We made some changes to our DLL structure, and to keep data files compatible we are using the serialization binder to remap types to other DLLs. This mostly works, but we have one issue with an Array of a custom type.

On serialization there is no call to BindToName( "CustomeType[]"...), I get several calls for BindToName for "CustomType", but none for the array. And as result I do not get the option to remap this type back to it's original DLL.

On deserialization I do get a call for BindToType( "CustomeType[]", ...) So there I can remap stuff/change things. But not on serialization.

When looking into the sources I found that the SerializationBInder.BindToName is called from BinaryObjectInfo.InitSerialize

And the code that does it is:

   if (objectType.IsArray)
   {
       InitNoMembers();
       return;
   }

   InvokeSerializationBinder(binder);

So indeed, if it is an array type, then the serialization binder is not invoked.

My guess is that moving the InvokeSerializationBinder to before the check on IsArray would solve the issue. But I do not know much about the inner workings of the serialization, so that may be to simple and break other things.

Reproduction Steps

  • Create a class marked as serializable and an array to it of a custom class.
  • Create your own serialization binder
  • Serialize an object to disk using a binary formatter and the custom serialization binder.

If this description is not enough, please ask for more info. I can create a small project to demonstrate the behavior if wanted.

Expected behavior

I would expect BindToName to be called for the array type "CustomeType[]"

Actual behavior

The BindToName function is not called for the array.

Regression?

I noticed this now when working on .Net 8, and the same code is present in the repository on GitHub.
According to the blame view in GitHub this code was added 8 years ago when the binary serialization was first added. So it looks like it has been there all the time.

Known Workarounds

None that I know of.

Configuration

.Net 8
Windows 11
x86.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions