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. C#
  4. Conditional Property Setters (friends in C#)

Conditional Property Setters (friends in C#)

Scheduled Pinned Locked Moved C#
csharpc++jsontutorialquestion
3 Posts 3 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006

    K S 2 Replies Last reply
    0
    • M Marc Clifton

      Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #2

      Well, I came across a way of doing this by accident sometime ago while I was exploring something else. However, I can't remember exactly what I did now! :(( Some combination of attributes and reflection. Kevin

      1 Reply Last reply
      0
      • M Marc Clifton

        Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006

        S Offline
        S Offline
        StealthyMark
        wrote on last edited by
        #3

        If you are using binary serialization, the visibility of the property doesn't matter, because the serializer uses the (private) fields. Xml serialization is different. You could use the OnDeserialized attribute to execute a method whoch sets a boolean flag. In the setter, check the flag and throw an exception.

        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