Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are so many simple types in the .Net framework not marked as serializable?

I find it a recurring inconvenience that a lot of simple types in the .Net framework are not marked as serializable. For example: System.Drawing.Point or Rectangle.

Both those structs only consist of primitive data and should be serializable in any format easily. However, because of the missing [System.Serializable] attribute, I can't use them with a BinaryFormatter.

Is there any reason for this, which I'm not seeing?

like image 399
Xarbrough Avatar asked May 17 '17 14:05

Xarbrough


1 Answers

It is simply a question of efficiency. Tagging a field as serializable the compiler must map each field onto a table of aliases. If they were all marked as serializables every object injecting or inheriting them need to be mapped aswell onto the table of aliases to process its serialization when probably you will never use them and it has a cost of memory and processing and it is more unsafe. Test it with millions of elements and you will see.

like image 147
Ixer Avatar answered Nov 02 '22 03:11

Ixer