我有这个接口和类(你可以看看here看到所有相关的代码是可编译的)。我想只为了说明问题(非编译代码)提供最少的代码:BinaryFormatter不反序列化IEnumerable上的对象
interface IViz<T> : ISerializable {
IEnumerable<SelectedValue> SelectedValues { get; }
};
[Serializable]
abstract class GroupViz<T, TIn, TOut> : IViz<T> {
public IEnumerable<SelectedValue> SelectedValues
{
get { return selectedValues.Cast<SelectedValue>(); }
}
}
[Serializable]
public class EntityValueGroupViz<TEntity, TKey> : ValueGroupViz<TEntity, TKey>
在第一界面(IViz
)我已经声明,其中存储在每个SelectedValue
对象的属性IEnumerable<SelectedValue> SelectedValues
。
SelectedValue
有两种实现方式(通用和非通用的):
[Serializable]
public abstract class SelectedValue : ISerializable
{
public SelectedValue(SerializationInfo info, StreamingContext context)
{
Configuration.SerializationTemplatesEnum serializationTemplateEnum = (Configuration.SerializationTemplatesEnum)context.Context;
foreach (SerializationEntry entry in info)
{
switch (serializationTemplateEnum)
{
case Configuration.SerializationTemplatesEnum.QUERY:
switch (entry.Name)
{
case "Value":
Value = entry.Value;
break;
case "Operator":
Operator = (VizOperatorEnum)entry.Value;
break;
}
break;
case Configuration.SerializationTemplatesEnum.TEMPLATE:
break;
}
}
}
}
[Serializable]
public class SelectedValue<T> : SelectedValue, ISerializable
{
public SelectedValue(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
以它们序列化我使用BinaryFormatter
,他们(上IViz.SelectValues
财产SelectedValue
对象)上连载文件。
但是,当我试图反序列化它们时,它们没有加载。我在SelectedValue(SerializationInfo info, StreamingContext context)
构造函数上添加了一个断点,但未达到。
我也试着在IViz.SelectedValues
属性上添加一个实现,我也尝试将该属性设置为IList
而不是IEnumerable
。但结果是一样的:我的SelectedValue
对象没有反序列化。
任何想法?
感谢您的意见。我试着添加每个最小的de /序列化相关代码,以便能够适合您的请求。它在[this](https://dotnetfiddle.net/bPYdJU)上。 – Jordi
好吧,如何反序列化'EntityValueGroupViz'中的'SelectedValues'有问题,但是[你的小提琴](https://dotnetfiddle.net/bPYdJU)中的测试代码不会构造和存储这种类型的实例它构造了一个'EntityQueryContext'的实例。在基类中似乎有一个'IList > vizs',但它不是公有的,也不会被填充。 –
dbc
谢谢。据我所知,你提出反序列化是有效的,但是我没有在任何地方达到我的'vizs'的内容(我认为这可能是一种代码错写)。我想建议询问为什么在序列化时到达SelectedValue.GetObjectData()时为什么在反序列化时到达了SelectedValue(SerializationInfo info,StreamingContext context)构造函数。我希望我解释得很好。 – Jordi