Alan,
I read that article you referenced and I'm not convinced that their method
has any advantages over this one. This perhaps puts a little more burden on
the class design, because it would have to reinitialize AFTER construction.
That's the only drawback that I can see.
I guess I need to ask: Do you see any value in defining readonly fields in
your classes?
Do you see any value in having the compiler enforce those readonly fields to
the class members themselves, as opposed to controlling the 'readonly'-ness
with a readonly property?
Do you see any value in having different instances having different values
for the readonly field.
Such a field may be Created, where the object has a date & time that it
entered a system, never to be changed during the lifetime (in memory or on
disk) of the object. Hence make the field itself immutable with the readonly
modifier. Which prevents the class itself from modifying the field. Yet
different instances of the class have different Created properties...
If you do, then you should see why defining ISerializable.SetObjectData
won't "cut it", and Serialization needs to use the constructor, as the
readonly field can only be initialized in the constructor.
If you don't see the value in immutable fields defined with readonly, then
yes I can see why you would think that ISerializable.SetObjectData is "good
enough".
Maybe its just that I like making immutable fields readonly, just for that
extra protection that the compiler won't let me modify the field...
Note although the above is blunt, it was not meant as a slam!
Hope this helps
Jay