简体   繁体   English

序列化继承自 ObservableObject 的 model

[英]Serialize model that inherits from ObservableObject

I want to serialize and deserialize a model object that inherits from MvvmLight.ObservableObject .我想序列化和反序列化从 MvvmLight.ObservableObject 继承的MvvmLight.ObservableObject Trying to deserialize the object with System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject throws a System.Runtime.Serialization.InvalidDataContractException with the info that the base class (ie MvvmLight.ObservableObject ) doensn't have the DataContractAttribute or SerializableAttribute . Trying to deserialize the object with System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject throws a System.Runtime.Serialization.InvalidDataContractException with the info that the base class (ie MvvmLight.ObservableObject ) doensn't have the DataContractAttribute or SerializableAttribute . My model that don't work looks like that:我的 model 不工作看起来像这样:

using GalaSoft.MvvmLight;

[DataContract]
public class MyModel : ObservableObject
{
  [IgnoreDataMember]
  private int _id;
  [DataMember]
  public int Id
  {
      get => _id;
      set => Set(ref _id, value);
  }
}

After removing [DataContract] there's no exception but also no deserialized data.删除[DataContract]后没有异常,也没有反序列化数据。

A solution could be to create a copy of MyModel called MyModelSerializable with the same properties but that don't inherit from ObservableObject and use that for serializing.一种解决方案可能是创建一个名为MyModelSerializableMyModel副本,该副本具有相同的属性,但不继承自 ObservableObject 并将其用于序列化。 After deserializing, the MyModel objects could be created with the data of the MyModelSerializable objects.反序列化后,可以使用MyModel对象的数据创建MyModelSerializable对象。 Is there a better solution?有更好的解决方案吗?

Edit: As requested a link to the code of GalaSoft.MvvmLight.ObservableObject : https://github.com/lbugnion/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(PCL)/ObservableObject.cs编辑:根据要求,链接到GalaSoft.MvvmLight.ObservableObject的代码: https://github.com/lbugnion/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(PCL)/ObservableObject.cs

You cant use DataContract Attribute here if you cant mark the ObservableObject with DataContract.如果不能用 DataContract 标记 ObservableObject,则不能在此处使用 DataContract 属性。 SO in your case i think you couldnt mark the ObservableObject.所以在你的情况下,我认为你不能标记 ObservableObject。

You have to Create another Class with the desired items you want to serialize without class derived ObservableObject您必须使用要序列化的所需项目创建另一个 Class 而无需 class 派生的 ObservableObject

Maybe You could implement (in another Serialized Class with DataContract Attribute which sees MyModel) in your project:也许您可以在您的项目中实现(在另一个带有 DataContract 属性的序列化 Class 中看到 MyModel):

    [OnSerializing]
    void PrepareForSerialization(StreamingContext sc)
    {
        //get items from MyModel and inject in new class serializable
    }

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

相关问题 当对象从列表继承时序列化对象 - Serialize object when the object inherits from list 从继承networkBehviour的类继承 - Inherits from a class that inherits networkBehviour C#如何在继承自DynamicObject的类上序列化(JSON,XML)常规属性 - C# How to serialize (JSON, XML) normal properties on a class that inherits from DynamicObject 初始化视图模型,该视图模型从基础模型继承并具有基础模型的值 - Initialise View Model that inherits from base model with values from base model 继承字典的Serialize类不是序列化属性 - Serialize class that inherits dictionary is not serializing properties 如何序列化继承WPF的段落的类型? - How to serialize a type that inherits WPF's Paragraph? 添加到视图模型的对象继承自单独的模型项目,是否破坏了MVVM添加引用? - Object added to view model inherits from seperate model project, does it break MVVM to add reference? 如何使用ASPX标记代码从用户控件中的Inherits模型访问对象 - How to access object from Inherits model in user control in ASPX markup code 将xml模型中的数据序列化为自定义二进制格式 - Serialize data from xml model into custom binary format 如何将 model(由 xsd.exe 构建)序列化为 XmlDocument? - How to serialize model (built from xsd.exe) to XmlDocument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM