I’m starting a new project on .NET 4. All the libraries I’ll be creating will have SecurityTransparentAttribute applied.
Now I have an immutable struct in one of these assemblies that needs to be serialized. As an immutable object, there won’t be setters for any property (like with System.DateTime).
If I simply mark the struct as [Serializable], nothing will be serialized, as the properties are readonly. The usual way to deal with this is to implement the ISerializable interface and take care of it in GetObjectData and the special constructor.
But in this case, my assembly will be security transparent, and ISerializable.GetObjectData is SecurityCritical, so I can’t do it this way.
So, what are my options here? I would really like to have everything SecurityTransparent because I won’t really need any critical stuff. Except this.
Thanks
If you’re using
BinaryFormatter, it serializes the fields of your struct, not its properties, and it works even if the fields arereadonly. So if it’s appropriate to serialize your struct by simply serializing its fields, you’re all set.