“调用的目标引发了异常。”在尝试反序列化对象时

时间:2010-11-27 17:09:53

标签: c# .net serialization

尝试在C#中反序列化对象时出现以下错误。

  

调用目标抛出了异常。

代码的相关部分:

    [Serializable()]
    public struct SlaAttribute : ISerializable
    {
        static int nextId = 1;

        public int id;
        public string parameterName;
        public string parameterNameInInitialTemplate;
        public string unit;

        public SlaAttribute(SerializationInfo info, StreamingContext ctxt)
        {
            this.id = (int)info.GetValue("id", typeof(int));
            this.parameterName = (string)info.GetValue("parameterName", typeof(string));
            this.parameterNameInInitialTemplate = (string)info.GetValue("parameterNameInInitialTemplate", typeof(string));
            this.unit = (string)info.GetValue("unit", typeof(string));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("id", typeof(int));
            info.AddValue("parameterName", typeof(string));
            info.AddValue("parameterNameInInitialTemplate", typeof(string));
            info.AddValue("unit", typeof(string));
        }
    }



[Serializable()]
class ConsumerSerialized : ISerializable
{
    string requirement;
    string dialect;
    int initialId;
    int id;
    int iteration;
    List<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute> attributes;

    public ConsumerSerialized(SerializationInfo info, StreamingContext ctxt)
    {
        this.requirement = (string)info.GetValue("requirement", typeof(string));
        this.dialect = (string)info.GetValue("dialect", typeof(string));
        this.initialId = (int)info.GetValue("initialId", typeof(int));
        this.id = (int)info.GetValue("id", typeof(int));
        this.iteration = (int)info.GetValue("iteration", typeof(int));
        this.attributes = (List<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>)info.GetValue("attributes", typeof(List<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>));
    }

    public ConsumerSerialized(Consumer consumer)
    {
        this.requirement = consumer.requirement;
        this.dialect = consumer.dialect;
        this.initialId = consumer.initialId;
        this.id = consumer.id;
        this.iteration = consumer.iteration;
        this.attributes = consumer.attributes;
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("requirement", typeof(string));
        info.AddValue("dialect", typeof(string));
        info.AddValue("initialId", typeof(int));
        info.AddValue("id", typeof(int));
        info.AddValue("iteration", typeof(int));
        info.AddValue("attributes", typeof(List<VieSLAF.WSLA.CaseStudy.Client.Console.Consumer.SlaAttribute>));
    }

    public Consumer Convert()
    {
        return new Consumer(requirement, dialect, initialId, id, iteration, attributes);
    }
}

[Serializable()]
class ListOfConsumers : ISerializable
{
    List<ConsumerSerialized> consumers = null;

    public List<ConsumerSerialized> Consumers
    {
        get { return consumers; }
        set { consumers = value; }
    }

    public ListOfConsumers(List<ConsumerSerialized> consumers)
    {
        this.consumers = consumers;
    }

    public ListOfConsumers(SerializationInfo info, StreamingContext ctxt)
    {
        this.consumers = (List<ConsumerSerialized>)info.GetValue("Consumers", typeof(List<ConsumerSerialized>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("Consumers", this.consumers);
    }
}

class ConsumersSerializer
{
    List<ConsumerSerialized> ConvertConsumers(List<Consumer> consumers)
    {
        List<ConsumerSerialized> result = new List<ConsumerSerialized>();
        foreach (Consumer c in consumers)
            result.Add(new ConsumerSerialized(c));

        return result;
    }

    List<Consumer> ConvertConsumers(List<ConsumerSerialized> consumers)
    {
        List<Consumer> result = new List<Consumer>();
        foreach (ConsumerSerialized c in consumers)
            result.Add(c.Convert());

        return result;
    }

    public void SerializeObject(List<Consumer> listOfConsumers)
    {
        ListOfConsumers consumers = new ListOfConsumers(ConvertConsumers(listOfConsumers));
        Stream stream = File.Open(Settings.FilePathSerializator, FileMode.Create);
        BinaryFormatter bFormatter = new BinaryFormatter();
        bFormatter.Serialize(stream, consumers);
        stream.Close();
    }

    public List<Consumer> DeSerializeObject()
    {
        try
        {
            Stream stream = File.Open(Settings.FilePathSerializator, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            ListOfConsumers consumers = (ListOfConsumers)bFormatter.Deserialize(stream);
            stream.Close();
            return ConvertConsumers(consumers.Consumers);
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.Message);
            throw ex;
        }
    }
}

我正在以这种方式进行序列化:

 List<Consumer> initialConsumers = ...
 ConsumersSerializer cs = new ConsumersSerializer();
 cs.SerializeObject(initialConsumers);

以这种方式反序列化:

ConsumersSerializer cs = new ConsumersSerializer();
List<Consumer> initialConsumers = cs.DeSerializeObject();

有什么想法吗?

感谢。 伊万

1 个答案:

答案 0 :(得分:12)

我犯了一个愚蠢的错误。而不是:

info.AddValue("id", this.id);

我写的是这样的:

info.AddValue("id", typeof(int));

最后一课 - 喝酒时不要做编程。

干杯。