c# - Dictionary<struct,int> 没有正确序列化

标签 c# wcf serialization dictionary

我有一个 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/

相关文章:

c# - 在 C# 中比较两个列表中的重复项

c# - WCF 返回 ArrayOfKeyValueOfintstringKeyValueOfintstring[] 而不是 Dictionary<int, string> 和 Array instread List

C# WCF MySQL 跟踪并发用户

C - 将指针数据保存/加载到文件

c# - NLayer MpegFile到SFML.Net SoundStream

c# - 需要在启动时按需将文件下载到 Azure Web 角色中

wpf - WCF/WPF 最佳实践 - WCF ChannelFactory 和 PRISM

c# - 反序列化期间未找到构造函数?

javascript - 如何将serializeArray更改为普通对象

c# - EF应该封装在基类中吗?