我有一个 WCF 服务,我尝试发送一个 Dictionary<CustomStruct, int>
从我的客户端到我的服务器。我的 WCF 服务使用默认的 BasicHttpBinding。
当我将词典发送到服务器时,没有抛出任何错误。但是当我尝试循环浏览我的词典时,它是空的。
奇怪的是Dictionary<string, string>
真的有用吗?!
有人知道为什么我的 Dictionary<CustomStruct, int>
通过电线后是空的,为什么Dictionary<string, string>
有效。
[编辑] 这是我的结构的样子:
[DataContract]
public struct CustomStruct : IEquatable<CustomStruct>
{
[DataMember]
private string _prop;
public string Prop { get { return _prop; } }
public override int GetHashCode()
{
return Prop.GetHashCode();
}
public static bool operator ==(CustomStruct left, CustomStruct right)
{
...
}
public static bool operator !=(CustomStruct left, CustomStruct right)
{
...
}
public override bool Equals(object obj)
{
...
}
}
最佳答案
这是一个可以满足您要求的工作示例
http://rocksolidknowledge.blob.core.windows.net/demos/CustomStructSerialization.zip
不幸的是,从您目前提供的代码来看,我看不出有什么问题 - 猜测您在客户端和服务的 CustomStruct 上有不同的命名空间,因此序列化无法正常工作,但没有看到生成的代码和自定义结构更完整我不能告诉
关于c# - Dictionary<struct,int> 没有正确序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6374315/