在使用谓词的名单DataContract SerializationException(DataCo

2019-09-20 02:06发布

我正在写将主要用于过滤从人口人一般的过滤器类。 我试图序列化过滤器类,但在运行时我得到一个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.

我的过滤器类如下所示:

[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));
        }
    }

当序列化过滤器类,我得到上面的例外。 如果我不标记谓词作为数据成员,那么它的工作原理。 但很明显,我想序列化属性也是如此。

我已经在这了几个小时了,我不能弄明白。 任何帮助将非常感激!

Answer 1:

DataContractSerializer不能用于序列化的代表; 一个多播代理是一组任意目标对象和方法; 这不是一个良好定义的面向数据的合同。 DataContractSerializer是用于数据

任一序列某种形式表达为字符串 (或一些简单的树)的,或使用不同的序列化器。



文章来源: DataContract SerializationException when using list of predicates