WCF:序列化和反序列化泛型集合 [英] WCF: Serializing and Deserializing generic collections
问题描述
我有一个一流的团队持有一个通用的清单:
I have a class Team that holds a generic list:
[DataContract(Name = "TeamDTO", IsReference = true)]
public class Team
{
[DataMember]
private IList<Person> members = new List<Person>();
public Team()
{
Init();
}
private void Init()
{
members = new List<Person>();
}
[System.Runtime.Serialization.OnDeserializing]
protected void OnDeserializing(StreamingContext ctx)
{
Log("OnDeserializing of Team called");
Init();
if (members != null) Log(members.ToString());
}
[System.Runtime.Serialization.OnSerializing]
private void OnSerializing(StreamingContext ctx)
{
Log("OnSerializing of Team called");
if (members != null) Log(members.ToString());
}
[System.Runtime.Serialization.OnDeserialized]
protected void OnDeserialized(StreamingContext ctx)
{
Log("OnDeserialized of Team called");
if (members != null) Log(members.ToString());
}
[System.Runtime.Serialization.OnSerialized]
private void OnSerialized(StreamingContext ctx)
{
Log("OnSerialized of Team called");
Log(members.ToString());
}
当我在一个WCF服务中使用这个类,我得到下面的日志输出
When I use this class in a WCF service, I get following log output
OnSerializing of Team called
System.Collections.Generic.List 1[XXX.Person]
OnSerialized of Team called
System.Collections.Generic.List 1[XXX.Person]
OnDeserializing of Team called
System.Collections.Generic.List 1[XXX.Person]
OnDeserialized of Team called
XXX.Person[]
反序列化后成员
是一个数组,不再是一个通用的清单,尽管字段类型为IList的&LT;>(?!)
当我尝试这个对象发回了WCF服务,我得到的日志输出
After the deserialization members
is an Array and no longer a generic list although the field type is IList<> (?!)
When I try to send this object back over the WCF service I get the log output
OnSerializing of Team called
XXX.Person[]
在此我的单元测试崩溃了System.ExecutionEngineException,这意味着WCF服务不能够序列化数组。 (也许是因为它预期的IList&LT;>)
After this my unit test crashes with a System.ExecutionEngineException, which means the WCF service is not able to serialize the array. (maybe because it expected a IList<>)
所以,我的问题是:是否有人知道为什么我的IList&LT的类型;>为反序列化后的数组,为什么我不能在那之后不再序列化我的球队对象
So, my question is: Does anybody know why the type of my IList<> is an array after deserializing and why I can't serialize my Team object any longer after that?
感谢
推荐答案
您已经遇到的的DataContractSerializer
陷阱之一。
You've run into one of the DataContractSerializer
gotchas.
修正:您的私有成员声明更改为:
Fix: Change your private member declaration to:
[DataMember]
private List<Person> members = new List<Person>();
或属性更改为:
OR change the property to:
[DataMember()]
public IList<Person> Feedback {
get { return m_Feedback; }
set {
if ((value != null)) {
m_Feedback = new List<Person>(value);
} else {
m_Feedback = new List<Person>();
}
}
}
和它的工作。微软连接错误是<一个href="https://connect.microsoft.com/wcf/feedback/details/433569/wcf-throws-an-executionengineexception-when-returning-an-array-as-ilist-t"相对=nofollow>这里
And it will work. The Microsoft Connect bug is here
当你反序列化对象发生此问题的的IList&LT; T&GT;
数据成员,然后尝试再次序列相同的实例
This problem occurs when you deserialize an object with an IList<T>
DataMember and then try to serialize the same instance again.
如果你想看到一些很酷的东西:
If you want to see something cool:
using System;
using System.Collections.Generic;
class TestArrayAncestry
{
static void Main(string[] args)
{
int[] values = new[] { 1, 2, 3 };
Console.WriteLine("int[] is IList<int>: {0}", values is IList<int>);
}
}
这将打印 INT []是的IList&LT; INT计算值:真
。
我怀疑这可能是你看到它回来作为反序列化后的数组的原因,但是这是不直观。
I suspect this is possibly the reason you see it come back as an array after deserialization, but it is quite non-intuitive.
如果你调用Add()方法的的IList&LT; INT&GT;
虽然阵,它抛出 NotSupportedException异常
。
If you call the Add() method on the IList<int>
of the array though, it throws NotSupportedException
.
其中一个.NET怪癖。
One of those .NET quirks.
这篇关于WCF:序列化和反序列化泛型集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!