title: Mark Event as NonSerializable for DataContractSerializer
body: <p>I have the following Event in my implementation of RelayCommand:</p>
  
  <pre><code>public event EventHandler CanExecuteChanged
  {
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
  }
  </code></pre>
  
  <p>Now I'm using <code>DataContractSerializer</code>. I know when using <code>DataContractSerializer</code> all Events have to be marked as <code>[field:NonSerialized]</code>. But in this case this doesn't work because my <code>CanExecuteChanged</code> Event is just a property with no private field.</p>
  
  <p>How can I mark this property as <code>NonSerializable</code>?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the whole RelayCommand class:</p>
  
  <pre><code>[DataContract]
  public class RelayCommand : ICommand
  {
  
    [field:NonSerialized]
    readonly Action&lt;object&gt; _execute;
  
    [field:NonSerialized]
    readonly Predicate&lt;object&gt; _canExecute;
  
    public RelayCommand(Action&lt;object&gt; execute)
      : this(execute, null)
    {
    }
  
    public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute)
    {
      if (execute == null)
        throw new ArgumentNullException("execute");
  
      _execute = execute;
      _canExecute = canExecute;
    }
  
    [DebuggerStepThrough]
    public bool CanExecute(object parameters)
    {
      return _canExecute == null ? true : _canExecute(parameters);
    }
  
    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
  
    public void Execute(object parameters)
    {
      _execute(parameters);
    }
  }
  </code></pre>
  
  <p>When I try to execute the <code>DataContractSerializer</code> I'm getting the following error:</p>
  
  <blockquote>
    <p>Type 'MyNamespace.RelayCommand' with data contract name 'RelayCommand:http://schemas.datacontract.org/2004/07/MyNamespace' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</p>
  </blockquote>