【问题标题】:Deserialize to self反序列化为自己
【发布时间】:2011-07-20 21:53:38
【问题描述】:

好的,我可能只是在这里遇到了史诗般的失败,但我想说这应该可行。

假设 DataProtect.DecryptData 将加密字符串作为输入,将解密字符串作为输出。假设 deserializeXML 生成适当的对象并从新解密的字符串中返回它。

所以。为什么这不起作用?

class ArrivedDetails
{
///...

    internal ArrivedDetails(string encrypted)
    {
        this = DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
    }
///...

给我一​​个错误

Cannot assign to '<this>' because it's read only

更具体地说,我怎样才能让它工作?我本质上是想解密对象的 XML 序列化版本,然后在构造函数中反序列化它。

我对“你不能”(有解释)持开放态度,因为我可以把它放在别处并分配值,但我的想法是这样的事情应该是可能的。

【问题讨论】:

    标签: c# serialization object


    【解决方案1】:

    不,这不可能使用构造函数,你不能重新分配this

    改用静态方法:

    public static ArrivedDetails CreateFromString(string encrypted)
    {
        return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
    }
    

    叫它:

    ArrivedDetails details = ArrivedDetails.CreateFromString(encrypted);
    

    【讨论】:

    • 虽然我同意 dotalchemy 认为原始概念应该有效,但静态方法可以解决问题。我喜欢你没有实例化一个新对象而只是返回它。
    • 这不会反序列化为 ,是吗?
    【解决方案2】:

    你想要的是一个创建你需要的对象的静态工厂方法。

    class ArrivedDetails
    {
    ///...
    
        public static ArrivedDetails CreateFromEncryptedKey(string encrypted)
        {
            return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
        }
    ///...
    

    您的初始方法不起作用的原因是this 是一个私有只读实例字段,它返回调用它的对象。你不能写信给this

    【讨论】:

      【解决方案3】:

      您不能为“this”分配任何内容。将 ArriveDetails 更改为返回反序列化对象的静态变量。

      class ArrivedDetails
      {
          static ArrivedDetails Create(string encrypted)
          { return DataProtect.deserializeXML(...) }
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用反射存档,如下所示。

        var tmp = DataProtect.deserializeXML(DataProtect.DecryptData(encrypted));
        foreach (var property in GetType().GetProperties())
            if (property.GetCustomAttributes(typeof (XmlIgnoreAttribute), false).GetLength(0) == 0)
                property.SetValue(this, property.GetValue(tmp, null), null);
        

        这会将反序列化的对象分配给一个时间变量,并通过反射将每个公共属性中的值复制到this。这个 sn-p 避免了复制带有 XmlIgnore 属性的属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-04-08
          • 2016-06-17
          • 2022-11-17
          • 1970-01-01
          • 2017-12-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多