title: How do I ignore event subscribers when serializing an object?
body: <p>When the following class is serialized with a <code>BinaryFormatter</code>, any objects subscribing to the <code>Roar</code> event will also be serialized, since references to those objects are held by the EventHandler delegate.</p>
  
  <pre><code>[Serializable]
  public class Lion
  {
      public event EventHandler Roar;
  
      public string Name { get; set; }
      public float Fluffiness { get; set; }
  
      public Lion(string name, float fluffiness)
      {
          Name = name;
          Fluffiness = fluffiness;
      }
  
      public void Poke()
      {
          Roar(); // Could be null, etc..
      }
  }
  </code></pre>
  
  <p><strong>How would you stop event subscribers being serialized as part of the object graph starting with a Lion?</strong></p>
  
  <p>Putting the <code>[NonSerializable]</code> attribute on the <code>event</code> will not compile.</p>
  
  <hr>
  
  <p>Note: I'm answering my own question since I think it might be useful to have the information on the site!</p>
  
  <blockquote>
    <p>FAQ: It's also perfectly fine to ask and answer your own question, but pretend you're on Jeopardy: phrase it in the form of a question.</p>
  </blockquote>