简体   繁体   English

属性序列化/反序列化

[英]Properties serialization/deserialization

I have a class which stores data collected by the asp.net webserver. 我有一个类,用于存储由asp.net网络服务器收集的数据。 It contains several properties like: 它包含几个属性,例如:

private string _actionName;
public string ActionName
    {
        get
        {
            if (_actionName == null)
                _actionName = Request.Params["action_name"] != null
                                  ? Server.UrlDecode(Request.Params["action_name"])
                                  : "";

            return _actionName;
        }
    }

This class is serialized to a file. 此类已序列化到文件。 Basically the data collected by webserver is written to files. 基本上,Web服务器收集的数据将写入文件。 Later these files are read and deserialized and the properties data needs to be imported to database offline. 以后,将读取并反序列化这些文件,并且需要将属性数据脱机导入数据库。

My question is whether the above code will serialize the properties correctly extracting data from query string and later when the data is deserialized the properties are correctly populated and returned? 我的问题是上面的代码是否将序列化属性,以便正确地从查询字符串中提取数据,并且稍后在对数据进行反序列化时,属性是否已正确填充并返回?

I am using binary serialization. 我正在使用二进制序列化。

Thanks in advance. 提前致谢。

Depending on the context of the serialization, the property ActionName will have a value or not when serializing and therefore will retrieve it or not after deserialization. 根据序列化的上下文,属性ActionName在序列化时将具有或没有值,因此在反序列化后将获取或没有值。

But you should not hope for Request.Params["action_name"] to get its value back if the source of the data was in there at the beginning of the process. 但是,如果在流程开始时数据源位于其中,则您不希望Request.Params [“ action_name”]取回其值。

If the instance gets serialized before the ActionName property gets read at least once, it will not serialize properly. 如果实例在至少读取一次ActionName属性之前被序列化,则它将无法正确序列化。 I would propose these changes, but even in this case you must be absolutelly sure that the serialization is occuring on the request thread (ie. so you have access to Request and Server). 我会提出这些更改,但是即使在这种情况下,您也必须绝对确保序列化正在请求线程上进行(即,您可以访问Request和Server)。

[NonSerialized]
private string _actionName; 
public string ActionName 
    { 
        get 
        { 
            if (_actionName == null) 
                _actionName = Request.Params["action_name"] != null 
                                  ? Server.UrlDecode(Request.Params["action_name"]) 
                                  : ""; 

            return _actionName; 
        } 
        set
        {
            _actionName= value;
         }
    }

This will work with Binary serialization if _actionName has a value. 如果_actionName有一个值,它将与二进制序列化一起工作。 Otherwise after deserialization if would look at the Request or Server objects to fetch the value again which might not be expected. 否则,在反序列化之后,将查看RequestServer对象以再次获取可能不期望的值。

I think that others has already advised you about caveats - so for serialization to work, you must a) Invoke each of property getter before serialization b) After deserialization, must modify each property field to some not null default value (eg String.Empty). 我认为其他人已建议您注意一些事项-因此要进行序列化,您必须a)序列化之前调用每个属性getter b)反序列化之后,必须将每个属性字段修改为一些非null的默认值(例如String.Empty) 。

Now if option b is feasible then 现在,如果选项b可行,则

  1. If you have only one such class the implement ISerializable and satisfy conditions a & b during serialization. 如果只有一个这样的类,则可以实现ISerializable并在序列化期间满足条件a和b。
  2. If you have many such classes then relatively easy solution could be to have a generic serialization surrogate for your types that will probably use reflection (to do property get and field set) and ensure that conditions a and b gets satisfied during serialization. 如果您有许多这样的类,那么相对简单的解决方案是为您的类型提供一个通用的序列化代理 ,该代理将可能使用反射(进行属性获取和字段设置),并确保在序列化期间满足条件a和b。

If b is not feasible then I am afraid that you don't have any other choice but to update property definitions. 如果b不可行,那么恐怕您别无选择,只能更新属性定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM