我在RelayCommand的实现中有以下事件:
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
现在我正在使用DataContractSerializer
.我知道在使用DataContractSerializer
时,所有事件都必须标记为[field:NonSerialized]
。但在这种情况下,这不起作用,因为我的CanExecuteChanged
事件只是一个没有私有字段的属性。
如何将此属性标记为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 的已知类型列表中。
这就是我从您在评论中给我的链接更新示例的方式:
[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);
}
}
}
它无一例外地工作。