'OptionalField' attribute
-
The meaning is writed as "If the member was not serialized, the CLR will leave the members's value null rather than throwing an exception." Visual Studio does the same thing as if this attribute was not used, so why should anyone use it? Or is it to garantee compatibility with .Net 1.x?
-
The meaning is writed as "If the member was not serialized, the CLR will leave the members's value null rather than throwing an exception." Visual Studio does the same thing as if this attribute was not used, so why should anyone use it? Or is it to garantee compatibility with .Net 1.x?
It is part of the changes made to serialization in .NET 2.0 to help alleviate the versioning issues that can be encountered when dealing with serialized objects. If you have version 1.0 of your product that serializes an object and then you create version 2.0 of your product that adds new fields, you would get an exception when you tried to deserialize the 1.0 version of the object. By marking the field with the OptionalField attribute, the object will be deserialized properly and the "missing" fields will be null.
----------------------------- In just two days, tomorrow will be yesterday.
-
It is part of the changes made to serialization in .NET 2.0 to help alleviate the versioning issues that can be encountered when dealing with serialized objects. If you have version 1.0 of your product that serializes an object and then you create version 2.0 of your product that adds new fields, you would get an exception when you tried to deserialize the 1.0 version of the object. By marking the field with the OptionalField attribute, the object will be deserialized properly and the "missing" fields will be null.
----------------------------- In just two days, tomorrow will be yesterday.
Strange, I can't simulate an exception, what am i doing wrong? 1. Create a class 2. Create an object instance 3. Serialize this object 4. Add a field to the class (value or reference) 5. Deserialize (I supposed .Net would trow an exception here because there's a new field that isn't serialized, but it doesn't) I think it's just .Net 1.x that generate an exception -- modified at 4:58 Wednesday 6th December, 2006
-
The meaning is writed as "If the member was not serialized, the CLR will leave the members's value null rather than throwing an exception." Visual Studio does the same thing as if this attribute was not used, so why should anyone use it? Or is it to garantee compatibility with .Net 1.x?
S O L V E D By using 'BinaryFormatter', the .Net Framework 2.0 doesn't give an exception if you do not use the 'OptionalField' attribute. But if u use the 'SoapFormatter', the meaning of the 'OptionalField' attribute becomes clear.