Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have OptionalField been obsoleted in C#?

Tags:

c#

.net

It seems that the OptionalFieldAttribute is not longer (if ever) needed for serialization with BinaryFormatter.

With a example from codeguru there is no longer any exception thrown when building the code in Visual Studio 2010, even when targeting older versions of the .NET framework.

Another question on stackoverflow suggests that it's always been optional. Is this true? And if so, is there actually not any way to get a exception for a missing field, when serializing to binary content in .NET?

like image 733
Claus Jørgensen Avatar asked Jan 27 '26 13:01

Claus Jørgensen


1 Answers

To get the exception, add this statement to the deserialization code:

        formatter.AssemblyFormat = FormatterAssemblyStyle.Full;
like image 118
Hans Passant Avatar answered Jan 30 '26 02:01

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!