在反序列化构造函数中的空字典 [英] Empty Dictionary in deserialization constructor

查看:84
本文介绍了在反序列化构造函数中的空字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用一个包含多个配置值的类。该类被序列化并存储在hd中供以后使用(基本上,它是一个序列化的配置文件)。

for an application I am using a class that holds several configuration values. That class is serialized and stored on hd for later use (basically, it is a serialized profile).

到目前为止,我已经使用了2个字典来存储一些数据。这很好用。但是,我们已经修改了配置,所以现在我不再使用字典了,而是另一个用来存储信息的对象。


为了能够使用较旧的配置文件,我的配置文件类现在正在实现ISerializable。我打算做的是在反序列化构造函数中检查数据是否存储为字典,如果是,则用字典中的数据手动填充
新对象。所以基本上情况是:

Up until now I have used 2 dictionaries to store some data. That worked just fine. However, we have modified the configuration, so that now I do not use dictionaries anymore but another object to store the info.
In order to be able to work with older profiles, I the profile class is now implementing ISerializable. What I intended to to do is to check, in the deserialization constructor, whether or not the data is stored as a dictionary and if so, manually fill the new object with the data from the dictionary. So basically, the situation is:

旧类:

[Serializable()]

公共类简介

{

   private Dictionary< string,string> connectionData;

   ....(其他一些数据)

  

  公开资料()

   {

        connectionData = new Dictionary< string,string>();

        ... ...
   }
}

[Serializable()]
public class Profile
{
   private Dictionary<string, string> connectionData;
   ....(some other data)
  
   public Profile()
   {
        connectionData = new Dictionary<string, string>();
        ...
   }
}

 

新课程:

[Serializable ()]

[Serializable()]

公共类简介:ISerializable

{

    private DBConnection connectionData; (存储数据的新课程)

    ... ...


    public GetObjectData(SerializationInfo info,StreamingContext context)

    {

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

        ... ...
    }

public class Profile : ISerializable
{
    private DBConnection connectionData; (new class to store the data)
    ...

    public GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("connectionData", this.connectionData);
        ...
    }

    公开资料()

    {

        ...

     public Profile()
    {
        ...

    }

    }

    protected Profile(SerializationInfo info,StreamingContext context)

    {

       试试
        {

            this.connectionData =(DBConnection)info.GetValue(" connectionData",typeof(DBConnection));

        }
        catch(InvalidCastException)
$
        {

           字典<字符串,字符串> tmp =(Dictionary< string,string>)info.GetValue(" connectionData",typeof(Dictionary< string,string>));
$
       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; this.connectionData = new DBConnection();

            this.connectionData.Server = tmp [" Server"];

            this.connectionData.Catalog = tmp [" Catalog"];

            ... ...
        }
    }
}



$
好​​的,所以基本计划有效。如果序列化配置文件是旧配置文件,我会捕获InvalidCastException,而是获取使用的旧字典。但是,我偶然发现了一个问题。字典是反序列化的,但是在Profile类的反序列化构造函数完成之后才会填充实际的
数据。也就是说,在构造函数内部,我无法访问字典,因为它有0个条目。然而,在反序列化构造函数完成之后,字典具有其所有
条目(我通过使用反序列化字典设置另一个变量来测试它)。我想这与字典本身实现iserializable这一事实有关。有没有办法从反序列化
构造函数中访问字典数据?

    protected Profile(SerializationInfo info, StreamingContext context)
    {
        try
        {
            this.connectionData = (DBConnection)info.GetValue("connectionData", typeof(DBConnection));
        }
        catch (InvalidCastException)
        {
            Dictionary<string, string> tmp = (Dictionary<string, string>)info.GetValue("connectionData", typeof(Dictionary<string, string>));
            this.connectionData = new DBConnection();
            this.connectionData.Server = tmp["Server"];
            this.connectionData.Catalog = tmp["Catalog"];
            ...
        }
    }
}


Okay, so the basic plan works. If the serialized profile is an old one, I catch the InvalidCastException and instead get the old dictionary that was used. However, here I stumbled upon a problem. The dictionary is deserialized, but is not filled with actual data until AFTER the deserialization constructor of the Profile class has completed. That is, inside the constructor I cannot access the dictionary since it has 0 entries. After the deserialization constructor is complete, however, the dictionary has all its entries (I tested this by setting another variable with the deserialized dictionary). I suppose this has something to do with the fact that the dictionary itself implements iserializable. Is there any way to access the dictionary data from inside the deserialization constructor?

推荐答案

昨天我花了几个小时调试同样的问题。要修复它,你需要在使用方法GetValue之后强制'dictionary.OnDeserialization(this);'。

I spent a few hours debugging the same issue yesterday. To fix it you need to force 'dictionary.OnDeserialization(this);' right after the method GetValue is used.

所以在你的示例代码中:

So in your sample code:

...

catch(InvalidCastException)

        {

           字典<字符串,字符串> tmp =(Dictionary< string,string>)info.GetValue(" connectionData",typeof(Dictionary< string,string>));

&NBSP;&NBSP;   tmp.OnDeserialization(this);

    tmp.OnDeserialization(this);



        &NBSP;&NBSP;&NBSP;&NBSP; this.connectionData = new DBConnection();

            this.connectionData.Server = tmp [" Server"];

            this.connectionData.Catalog = tmp [" Catalog"];

            ... ...
        }

...


这篇关于在反序列化构造函数中的空字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆