Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. 'OptionalField' attribute

'OptionalField' attribute

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdotnetvisual-studioquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hendrik Debedts
    wrote on last edited by
    #1

    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 H 2 Replies Last reply
    0
    • H Hendrik Debedts

      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 Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • S Scott Dorman

        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.

        H Offline
        H Offline
        Hendrik Debedts
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • H Hendrik Debedts

          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?

          H Offline
          H Offline
          Hendrik Debedts
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups