[DataContract SerializationException使用谓词列表时

问题描述 投票:2回答:2

我正在编写一个通用过滤器类,该类将主要用于过滤人群中的人员。我正在尝试序列化过滤器类,但是在运行时我得到了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));
        }
    }

序列化过滤器类时,出现上述异常。如果我没有将谓词标记为DataMember,那么它将起作用。但是显然我也想序列化该属性。

我已经在这里待了两个小时,我无法弄清楚。任何帮助将不胜感激!

c# serialization datacontractserializer
2个回答
4
投票

DataContractSerializer不适用于序列化委托;多播委托是一组任意目标对象和方法;这是定义明确的面向数据的合同。 DataContractSerializer用于数据要么序列化某种形式的表达式

作为字符串

(或一些简单的树),要么使用其他序列化器。


0
投票
[不尝试复活死角,但我搜索(基本上)相同的错误消息,并且此问题当前是排名第一的结果,但是我不尝试对委托者做任何事情!
© www.soinside.com 2019 - 2024. All rights reserved.