gpt4 book ai didi

c# - 使用谓词列表时出现 DataContract SerializationException

转载 作者:行者123 更新时间:2023-11-30 21:05:36 26 4
gpt4 key购买 nike

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

当序列化过滤器类时,出现上述异常。如果我不将 Predicate 标记为 DataMember,那么它就可以工作。但显然我也想序列化该属性。

我已经处理这个问题几个小时了,但我想不通。任何帮助将不胜感激!

最佳答案

DataContractSerializer 不适用于序列化委托(delegate);多播委托(delegate)是一组任意目标对象和方法;这不是一个明确定义的面向数据的契约。 DataContractSerializer 用于数据

要么将某种形式的表达式序列化为字符串(或某种简单的树),要么使用不同的序列化程序。

关于c# - 使用谓词列表时出现 DataContract SerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com