Answer the question
In order to leave comments, you need to log in
How to serialize ObservableCollection interface?
Hello.
I have a module that implements an interface
[DataContract]
class LineModule1VM : BaseVM, ILineModule1
{
[DataMember]
public ObservableCollection<FoundLine> FoundDataLines { get; set; } = new ObservableCollection<FoundLine>();
public ObservableCollection<Line> Lines { get; set; }
interface ILineModule1 : ILineModule
{
[DataMember]
public double D { get; set; }
}
interface ILineModule
{
[DataMember]
public ObservableCollection<FoundLine> FoundDataLines { get; set; }
[DataMember]
public ObservableCollection<Line> Lines { get; set; }
}
[DataContract]
[KnownType(typeof(LineModule1VM))]
class ListBoxItemVM : BaseVM
{
[DataMember]
public ObservableCollection<ILineModule1> lineModule1Items { get; set; } = new ObservableCollection<ILineModule1>();
}
using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create))
{
var ser = new DataContractSerializer(typeof(DataProgram));
ser.WriteObject(fs, DataToSave);
}
using FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open);
var serTest = new DataContractSerializer(typeof(DataProgram));
var DataTest = (DataProgram)serTest.ReadObject(fs);
Вызвано исключение: "System.Runtime.Serialization.SerializationException" в System.Private.DataContractSerialization.dll
Необработанное исключение типа "System.Runtime.Serialization.SerializationException" в System.Private.DataContractSerialization.dll
Type 'Baser.ViewModels.Module1.LineModule1VM' with data contract name 'LineModule1VM :http://schemas.datacontract.org/2004/07/Baser.ViewModels.Modules1' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question