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. Mark field in child class as NonSerialized

Mark field in child class as NonSerialized

Scheduled Pinned Locked Moved C#
helpquestionlearning
8 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.
  • D Offline
    D Offline
    Dust Signs
    wrote on last edited by
    #1

    Hi, as the title indicates I want to mark a field in a child class with a NonSerialized attribute so that it won't be serialized. Simply put:

    [Serializable]
    class Parent
    {
    protected int SomeField;
    }

    [Serializable]
    class Child : Parent
    {
    [NonSerialized]
    protected int SomeField;
    }

    Of course this does not work, but also the new keyword does not help me because it only hides the field and does not overwrite it. Is there any other way to achieve the desired effect? Regards Dust Signs

    The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

    D 1 Reply Last reply
    0
    • D Dust Signs

      Hi, as the title indicates I want to mark a field in a child class with a NonSerialized attribute so that it won't be serialized. Simply put:

      [Serializable]
      class Parent
      {
      protected int SomeField;
      }

      [Serializable]
      class Child : Parent
      {
      [NonSerialized]
      protected int SomeField;
      }

      Of course this does not work, but also the new keyword does not help me because it only hides the field and does not overwrite it. Is there any other way to achieve the desired effect? Regards Dust Signs

      The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

      D Offline
      D Offline
      deyaert
      wrote on last edited by
      #2

      I thought NonSerialized is intended for this purpose, or what whould you expect?

      D 1 Reply Last reply
      0
      • D deyaert

        I thought NonSerialized is intended for this purpose, or what whould you expect?

        D Offline
        D Offline
        Dust Signs
        wrote on last edited by
        #3

        Hi, of course you are right, but I need this as the parent class saves information in this field which the child classes do not need anymore because they compute it dynamically. And of course we are not talking about an int field, but about an array of a more complex data type which would consume up to 20 or 30 KB per instance which would be serialized and "thrown away" after deserialization as it has to be recomputed anyways. I hope this helps to better understand the purpose of what I want to achieve. Dust Signs

        The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

        D 1 Reply Last reply
        0
        • D Dust Signs

          Hi, of course you are right, but I need this as the parent class saves information in this field which the child classes do not need anymore because they compute it dynamically. And of course we are not talking about an int field, but about an array of a more complex data type which would consume up to 20 or 30 KB per instance which would be serialized and "thrown away" after deserialization as it has to be recomputed anyways. I hope this helps to better understand the purpose of what I want to achieve. Dust Signs

          The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

          D Offline
          D Offline
          deyaert
          wrote on last edited by
          #4

          If you don't want to store it, but on deserialization want to get default values. Then maybe the OnDeserializedAttribute can be of use... http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

          D 1 Reply Last reply
          0
          • D deyaert

            If you don't want to store it, but on deserialization want to get default values. Then maybe the OnDeserializedAttribute can be of use... http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

            D Offline
            D Offline
            Dust Signs
            wrote on last edited by
            #5

            Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs

            The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

            D D 2 Replies Last reply
            0
            • D Dust Signs

              Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs

              The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

              D Offline
              D Offline
              deyaert
              wrote on last edited by
              #6

              maybe something like this could be usefull?! [OnDeserializing()] internal void OnDeserializingMethod(StreamingContext context) { this.nonserializedmember = reInitialise(); foreach (Child c in this.childs) { child.newNonserializedValue = this.nonserializedmember; } }

              1 Reply Last reply
              0
              • D Dust Signs

                Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs

                The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

                D Offline
                D Offline
                Dust Signs
                wrote on last edited by
                #7

                I just answered this question myself :). I just had to use OnSerializing and OnSerialized to set the field to null and reset it to its old value respectively. Thank you very much for your help. Regards, Dust Signs

                The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

                D 1 Reply Last reply
                0
                • D Dust Signs

                  I just answered this question myself :). I just had to use OnSerializing and OnSerialized to set the field to null and reset it to its old value respectively. Thank you very much for your help. Regards, Dust Signs

                  The number you dialed is imaginary. Please turn your phone by 90 degrees and try again

                  D Offline
                  D Offline
                  deyaert
                  wrote on last edited by
                  #8

                  No prob :)

                  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