0

我在 RelayCommand 的实现中有以下事件:

public event EventHandler CanExecuteChanged
{
  add { CommandManager.RequerySuggested += value; }
  remove { CommandManager.RequerySuggested -= value; }
}

现在我正在使用DataContractSerializer. 我知道在使用DataContractSerializer所有事件时必须将其标记为[field:NonSerialized]. 但在这种情况下,这不起作用,因为我的CanExecuteChangedEvent 只是一个没有私有字段的属性。

如何将此属性标记为NonSerializable

编辑:

这是整个 RelayCommand 类:

[DataContract]
public class RelayCommand : ICommand
{

  [field:NonSerialized]
  readonly Action<object> _execute;

  [field:NonSerialized]
  readonly Predicate<object> _canExecute;

  public RelayCommand(Action<object> execute)
    : this(execute, null)
  {
  }

  public RelayCommand(Action<object> execute, Predicate<object> 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);
  }
}

当我尝试执行时,DataContractSerializer出现以下错误:

不应使用数据合同名称“RelayCommand:http://schemas.datacontract.org/2004/07/MyNamespace”键入“MyNamespace.RelayCommand”。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

4

1 回答 1

1

这就是我从您在评论中给我的链接更新示例的方式:

[Serializable]
public abstract class BaseClass
{
    public string Name { get; set; }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

public class DerivedClass : BaseClass
{
    public int Age { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        var derivedClass = new DerivedClass { Name = "Test", Age = 10 };
        derivedClass.CanExecuteChanged += (sender, eventArgs) => Console.WriteLine("hello");

        var serializer = new DataContractSerializer(typeof(DerivedClass));
        using (var stream = new FileStream("d:\\test.txt", FileMode.Create, FileAccess.ReadWrite))
        {
            serializer.WriteObject(stream, derivedClass);
        }
    }
}

它毫无例外地工作。

于 2012-07-09T08:53:31.767 回答