title: How to exclude an attribute when serializing C#-object
body: <p>How can I prevent an attribute from being serialized? I have an abstract class and the class is marked with [Serializable]. But I don't want the attribute Checksum to be included in the serialization. How do I do that?</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Runtime.Serialization;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.Security.Cryptography;
  
  namespace test
  {
  
      [DataContract]
      [Serializable]
      public abstract class Baseclass
      {
          /// &lt;summary&gt;
          /// The product id.
          /// &lt;/summary&gt;
          [DataMember(Name = "productId")]
          public int ProductId { get; set; }
  
          /// &lt;summary&gt;
          /// The calculated checksum for this object. 
          /// It is calculated using SHA1.
          /// For more information about SHA-1, see https://en.wikipedia.org/wiki/SHA-1
          /// &lt;/summary&gt;
          [DataMember(Name = "checksum")]
          public long Checksum
          {
              get
              {
                  long tempHash = 0;
                  try
                  {
                      this.temp = 0;
                      var seraializer = new BinaryFormatter();
                      var sha1 = new SHA1Managed();
                      byte[] buffer;
  
                      using (var memoryStream = new MemoryStream())
                      {
                          seraializer.Serialize(memoryStream, this);
                          buffer = memoryStream.ToArray();
                      }
                      var bArrayHash = sha1.ComputeHash(buffer);
                      tempHash = Math.Abs(BitConverter.ToInt64(bArrayHash, 0));
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex);
                  }
                  return tempHash;
              }
          }
      }
  }
  </code></pre>
  
  <p>I have tried the following code from MSDN (<a href="https://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx</a>) but when I try to apply it to my code, the complier says that it is not valid on this declaration type, and that it is only valid on 'field' declarations.</p>
  
  <pre><code>// A test object that needs to be serialized.
  [Serializable()]        
  public class TestSimpleObject  {
  
      public int member1;
      public string member2;
      public string member3;
      public double member4;
  
      // A field that is not serialized.
      [NonSerialized()] public string member5; 
  
      public TestSimpleObject() {
  
          member1 = 11;
          member2 = "hello";
          member3 = "hello";
          member4 = 3.14159265;
          member5 = "hello world!";
      }
  
  
      public void Print() {
  
          Console.WriteLine("member1 = '{0}'", member1);
          Console.WriteLine("member2 = '{0}'", member2);
          Console.WriteLine("member3 = '{0}'", member3);
          Console.WriteLine("member4 = '{0}'", member4);
          Console.WriteLine("member5 = '{0}'", member5);
      }
  }
  </code></pre>