如何序列化Dictionary>使用 DataContractSerializer?

发布于 2024-11-28 00:31:02 字数 296 浏览 1 评论 0 原文

当我尝试序列化此类时,出现 CommunicationException。 我是否需要使用任何特定的 CollectionDataContract 标记才能使其正常工作?

[DataContract()]
public class MyClass
{
  [DataMember()]
  public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
}

When I try to have this class serialized I get a CommunicationException.
Do I need to use any specific CollectionDataContract markup to get this working?

[DataContract()]
public class MyClass
{
  [DataMember()]
  public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱*していゐ 2024-12-05 00:31:02

这应该可行(参见下面的示例)。您得到的完整异常是什么?

public class StackOverflow_6966835
{
    [DataContract()]
    public class MyClass
    {
        [DataMember()]
        public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
    }
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        MyClass GetMyClass();
    }
    public class Service : ITest
    {
        public MyClass GetMyClass()
        {
            return new MyClass
            {
                dictionary = new Dictionary<string, List<string>>
                {
                    { "one", "uno un eins".Split().ToList() },
                    { "two", "dos deux zwei".Split().ToList() },
                    { "three", "tres trois drei".Split().ToList() }
                }
            };
        }
    }
    public static void Test()
    {
        Console.WriteLine("Stand-alone serialization");
        MemoryStream ms = new MemoryStream();
        MyClass c = new Service().GetMyClass();
        DataContractSerializer dcs = new DataContractSerializer(typeof(MyClass));
        dcs.WriteObject(ms, c);
        Console.WriteLine("Serialized: {0}", Encoding.UTF8.GetString(ms.ToArray()));

        Console.WriteLine();
        Console.WriteLine("Now using in a service");

        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.GetMyClass());

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

This should just work (see examples below). What's the full exception which you get?

public class StackOverflow_6966835
{
    [DataContract()]
    public class MyClass
    {
        [DataMember()]
        public Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
    }
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        MyClass GetMyClass();
    }
    public class Service : ITest
    {
        public MyClass GetMyClass()
        {
            return new MyClass
            {
                dictionary = new Dictionary<string, List<string>>
                {
                    { "one", "uno un eins".Split().ToList() },
                    { "two", "dos deux zwei".Split().ToList() },
                    { "three", "tres trois drei".Split().ToList() }
                }
            };
        }
    }
    public static void Test()
    {
        Console.WriteLine("Stand-alone serialization");
        MemoryStream ms = new MemoryStream();
        MyClass c = new Service().GetMyClass();
        DataContractSerializer dcs = new DataContractSerializer(typeof(MyClass));
        dcs.WriteObject(ms, c);
        Console.WriteLine("Serialized: {0}", Encoding.UTF8.GetString(ms.ToArray()));

        Console.WriteLine();
        Console.WriteLine("Now using in a service");

        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        Console.WriteLine(proxy.GetMyClass());

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文