DataContract使用谓词列表时发生SerializationException [英] DataContract SerializationException when using list of predicates
问题描述
我正在写将主要用于过滤从人口人一般的过滤器类。我试图序列化过滤器类,但在运行时我得到一个SerializationException:
I'm writing a generic filter class that will be primarily used to filter persons from a population. I'm trying to serialize the filter class, but at runtime I get an SerializationException:
System.Runtime.Serialization.SerializationException : Type 'System.DelegateSerializationHolder+DelegateEntry' with data contract name 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or 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.
我的过滤器类看起来是这样的:
My filter class looks like this:
[DataContract(Name = "Filter", Namespace = "")]
public class Filter<T>
{
/// <summary>
/// Default constructor, needed by serializers.
/// </summary>
public Filter()
{
Name = "New filter";
Predicates = new List<Predicate<T>>();
}
/// <summary>
/// ctor. Takes a list of predicates for type T
/// to filter with.
/// </summary>
public Filter(string name, IEnumerable<Predicate<T>> predicates)
{
Name = name;
Predicates = predicates.ToList();
}
/// <summary>
/// Name of the filter.
/// </summary>
[DataMember(Order = 0)]
public string Name
{
get;
set;
}
[DataMember(Order = 1)]
public List<Predicate<T>> Predicates
{
get;
set;
}
/// <summary>
/// Filters sequence of type T.
/// </summary>
public IEnumerable<T> ApplyFilter(IEnumerable<T> input)
{
var result = new List<T>(input);
return Predicates.Aggregate(result, (current, predicate) => current.FindAll(predicate));
}
}
在序列化过滤器类,我得到了上面的异常。如果我没有标记谓词作为数据成员,那么它的工作原理。但很明显,我想序列化属性也是如此。
When serializing the filter class, I get the above exception. If I don't mark the Predicate as a DataMember, then it works. But obviously I want to serialize that property as well.
我已经在这了几个小时了,我不能弄明白。任何帮助将非常感激!
I've been at this for a couple of hours now, and I can't figure it out. Any helps would be really appreciated!
推荐答案
的DataContractSerializer
不适用于序列化的代表;多播委托是一组任意目标对象和方法;这是不会一个定义良好的面向数据的合同。 的DataContractSerializer
面向的数据的
DataContractSerializer
is not intended for serializing delegates; a multi-cast delegate is a set of arbitrary target objects and methods; this is not a well-defined data-oriented contract. DataContractSerializer
is intended for data.
无论是序列化的一些表现形式的作为一个字符串的(或一些简单的树),或使用不同的序列化。
Either serialize some form of expression as a string (or some simple tree), or use a different serializer.
这篇关于DataContract使用谓词列表时发生SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!