Repository navigation
Create BinaryFormatter replacement for known types - #9088
Conversation
In a number of cases we know what data we're serializing or deserializing and can avoid using the BinaryFormatter if we weren't concerned about compatibility. I was considering using a different format on the wire and writing a parser to deal with legacy data. It finally occured to me that if I'm going to parse legacy data I might as well put the data out in the legacy format to maintain compatibility. This creates a model for the NRBF format that can be used to look at data the BinaryFormatter puts out. The top level of the model is represented by the BinaryFormattedObject class. The binary records that are part of the model all self serialize and deserialize, which allows manually constructing known models to export data that can be read by the BinaryFormatter. The records never hydrate any complex types other than strings, that must be done manually. BinaryFormatReader and BinaryFormatWriter have helper methods for creating and reading a set of known types. I've handled primitive types and Hashtable and started on List<T> as well. Clipboard will no longer use the BinaryFormatter for primitive types. Tests have been updated appropriately. My next step is to change the ActiveX property bag serialization to use the Hashtable serialization that this change introduces. I also have a few more tests to port from my prototype repo. The idea is to then further reduce the need to fall back on the BinaryFormatter by providing support for other BinaryFormatted types in WinForms and other types that WinForms projects would commonly use as public property types. This includes things like PointF and RectangleF, primitive arrays, and other collections of primitive types (List<T> and ArrayList are the priorities there). BinaryFormattedObject does not yet support multidimensional or jagged arrays.
|
There are a little over 500 lines of executable code to implement this. I expect it will total 600 or so when the other items are implemented. I've tried to keep the maintainability index high (currently 84). I've also leaned heavily on patterns to help avoid parsing mistakes. Usage of BinaryReader/Writer takes a fair amount of validation off of our hands. I've encapsulated counts and identifiers into structs that enforce positive numbers. I've also guarded when reading data from stream provided counts. |
…iter. Add another check for DateTime and try/catch for clipboard.
Tanya-Solyanik
left a comment
There was a problem hiding this comment.
I didn't finish, will get back to it later.
| "ticks", "dateData" | ||
| }; | ||
|
|
||
| private const string Mscorlib = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; |
There was a problem hiding this comment.
Had you tested copy/pasting between .NET Framework applications and Core applications? At some point it used to work. Perhaps a unit test to test it can be added?
There was a problem hiding this comment.
Not explicitly, but the BinaryFormatter uses the mscorlib versions (when it puts out a version). Everything I've done here is matching what it puts out.
In a number of cases we know what data we're serializing or deserializing and can avoid using the BinaryFormatter if we weren't concerned about compatibility.
I was considering using a different format on the wire and writing a parser to deal with legacy data. It finally occured to me that if I'm going to parse legacy data I might as well put the data out in the legacy format to maintain compatibility.
This creates a model for the NRBF format that can be used to look at data the BinaryFormatter puts out. The top level of the model is represented by the BinaryFormattedObject class.
The binary records that are part of the model all self serialize and deserialize, which allows manually constructing known models to export data that can be read by the BinaryFormatter. The records never hydrate any complex types other than strings, that must be done manually.
BinaryFormatReader and BinaryFormatWriter have helper methods for creating and reading a set of known types. I've handled primitive types and Hashtable and started on List as well.
Clipboard will no longer use the BinaryFormatter for primitive types. Tests have been updated appropriately.
My next step is to change the ActiveX property bag serialization to use the Hashtable serialization that this change introduces. I also have a few more tests to port from my prototype repo.
The idea is to then further reduce the need to fall back on the BinaryFormatter by providing support for other BinaryFormatted types in WinForms and other types that WinForms projects would commonly use as public property types. This includes things like PointF and RectangleF, primitive arrays, and other collections of primitive types (List and ArrayList are the priorities there).
Note that performance isn't far off from using the BinaryFormatter. For serializing a really large Hashtable (around 10K records) we are about the same speed and use a slightly larger amount of memory. It's possible it could be significantly better if we use the enumerator to get the data instead of ISerializable- would have to spend some time to figure out the right values for the other private fields without using reflection.
BinaryFormattedObject does not yet support multidimensional or jagged arrays. This doesn't hit any of our scenarios, will implement when needed.
This change also consumes FluentAssertions in the Primitives test assembly. We've discussed using this as a team and will be looking at it in use here before committing to the rest of the test projects.
Microsoft Reviewers: Open in CodeFlow