Всем привет.
Есть у меня модуль реализующий интерфейс
[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; }
}
и есть основная VM
[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.
Не пойму как мне задать KnownTypeAttribute чтобы заработало. Что не так?