【问题标题】:Serializing Exception - Cannot be serialized because it does not have a parameterless constructor [duplicate]序列化异常-无法序列化,因为它没有无参数构造函数[重复]
【发布时间】:2013-05-26 16:48:38
【问题描述】:

在初始化 XMLSerializer 并将 Appointment 的实例(来自 EWS API)传递给它时遇到异常:

Microsoft.Exchange.WebServices.Data.Appointment 无法序列化,因为它没有无参数构造函数。

这是我的代码:

private static string FindAppointmentsAsXmlString(CalendarView calendar, ExchangeService serv)
{
    FindItemsResults<Appointment> appointments = serv.FindAppointments(
        WellKnownFolderName.Calendar, calendar);
    List<Appointment> appointmentsList = appointments.ToList();

    var serializer = new XmlSerializer(appointmentsList.GetType());
    var writer = new StringWriter();

    try
    {
        serializer.Serialize(writer, appointmentsList);
        Console.WriteLine(writer.GetStringBuilder().ToString());
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        Console.ReadLine();
    }

    return writer.GetStringBuilder().ToString(); 
}

【问题讨论】:

  • @GrantWinney 我不明白这对我有什么帮助,除非你能提供一些见解?
  • ...我刚刚再次运行我的应用程序,它是“无参数的”。我的错!啊!

标签: c# xml serialization exchangewebservices xmlserializer


【解决方案1】:

如果您使用 AutoMapper,请通过 Nuget 或 https://github.com/AutoMapper/AutoMapper 获取 如果你这样做:-

namespace DTO
    {        
        public class CalendarAppointment : Appointment
        {
            public CalendarAppointment() : base( /* any parameters to construct the base appointment, doesn't matter /* )
            {
            }
        }
}

那你就可以了

private static string FindAppointmentsAsXmlString(CalendarView calendar, ExchangeService serv)
        {
            FindItemsResults<Appointment> appointments = serv.FindAppointments(
                WellKnownFolderName.Calendar, calendar);

            Mapper.CreateMap<Appointment, DTO.CalendarAppointment>();

            var list = appointments.Select(Mapper.Map<DTO.CalendarAppointment>).ToList();
            var serializer = new XmlSerializer(list.GetType());
            var writer = new StringWriter();

            try
            {
                serializer.Serialize(writer, list);
                Console.WriteLine(writer.GetStringBuilder().ToString());
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }

            return writer.GetStringBuilder().ToString();
        }

【讨论】:

    【解决方案2】:

    我在上面的评论中链接到post 的原因(已删除) 是因为它声明:

    在对象的反序列化过程中,负责反序列化对象的类会创建序列化类的实例,然后只有在获取要填充的实例后才能继续填充序列化的字段和属性。

    Appointment constructor的文档可以看出Appointment在构造函数中需要ExchangeService的实例...没有无参数的构造函数,因此报错。

    我不了解序列化过程的所有内部工作原理,但希望这至少可以为您提供错误的原因。

    通常,如果它是您自己的类,您应该能够添加一个额外的(无参数)构造函数,但我不确定在这种情况下您会做什么。很可能还有另一种方法可以成功序列化Appointment 类,但我不确定。

    您可以尝试扩展 Appointment 类,在其中提供一个无参数构造函数,然后使用 XML 对其进行序列化。我不确定它是否会起作用,但也许其他人可以提供更明确的答案。

    public class MyAppointment : Appointment
    {
        public MyAppointment() { }
    }
    

    如果我在上面找到任何其他内容,我会更新。

    【讨论】:

    • 感谢格兰特,非常有帮助!我在将内容写入 XML 文件时正在序列化 Appointment 对象,是否有另一种方法可以避免此异常,将对象的内容写入 XML 文件?
    • 感谢您的帮助@GrantWinney,我确实有另一种想法 - 遍历类型约会列表,将每个节点转换为字符串,然后序列化?此外,在实现扩展 Appointment 的类时,结果是:“'Microsoft.Exchange...Appointment' 不包含采用 0 个参数的构造函数”
    • 我会试一试,我不明白这怎么行。干杯!
    猜你喜欢
    • 2015-12-23
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    相关资源
    最近更新 更多