2016-04-23 1 views
0

Windows 10 application universelle. VBSérialisation d'une erreur de cause de classe

J'ai la classe ci-dessous que je veux enregistrer au fichier et lire à partir du fichier, l'enregistrement fonctionne mais quand j'essaye de charger je reçois l'erreur.

Message = erreur dans la ligne 1 la position 249. Élément 'http://schemas.datacontract.org/2004/07/KoW_Universal_v2:MagicItem' contient des données de contrat de données 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfKeyValueOfstringMagicItemyoeMIiQz. Le désérialiseur n'a aucune connaissance de quelque type que ce soit correspondant à ce contrat. Ajoutez le type correspondant à 'ArrayOfKeyValueOfstringMagicItemyoeMIiQz' à la liste des types connus - par exemple, en utilisant l'attribut KnownTypeAttribute ou en l'ajoutant à la liste des types connus transmis à DataContractSerializer. Source = System.Private.DataContractSerialization

Imports System.Runtime.Serialization 
Imports Windows.Storage 

Public Class MagicItem 
    Property MagicItemName As String 
    Property MagicItemCost As Integer 
    Property MagicItemDescripyion As String 
    Property LimitToHero As Boolean 
    Property LimitToSpecialRule As String 'key to the special rule 

End Class 



Public Class clsMagicItems 
    Private MagicItems As New Dictionary(Of String, MagicItem) 

Async Sub SaveMagicItems() 
    ' StorageFile File = Await openPicker.PickSingleFileAsync(); 
    Dim myfile As StorageFile 
    myfile = Await ApplicationData.Current.LocalFolder.CreateFileAsync("MagicItems.dat", CreationCollisionOption.ReplaceExisting) 

    Dim KnownTypeList As New List(Of Type) 
    KnownTypeList.Add(GetType(clsMagicItems)) 
    KnownTypeList.Add(GetType(MagicItem)) 

    Dim r As Streams.IRandomAccessStream 
    r = Await myfile.OpenAsync(FileAccessMode.ReadWrite) 
    Using outStream As Streams.IOutputStream = r.GetOutputStreamAt(0) 
     Dim serializer As New DataContractSerializer(GetType(clsMagicItems), KnownTypeList) 
     serializer.WriteObject(outStream.AsStreamForWrite(), MagicItems) 
     Await outStream.FlushAsync() 
     outStream.Dispose() 
     r.Dispose() 
    End Using 
End Sub 
Async Sub LoadMagicItems() 
    Try 
     Dim myfile As Stream 

     Dim KnownTypeList As New List(Of Type) 
     KnownTypeList.Add(GetType(clsMagicItems)) 
     KnownTypeList.Add(GetType(MagicItem)) 

     Dim serializer As New DataContractSerializer(GetType(clsMagicItems), KnownTypeList) 
     myfile = Await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("MagicItems.dat") 
     'LINE BELOW RAISE ERROR 
     MagicItems = serializer.ReadObject(myfile) 
    Catch 
     'failed to load the file 
    End Try 
End Sub 
End Class 

Répondre

0

le fixe, aurait dû avoir le code suivant

KnownTypeList.Add(GetType(Dictionary(Of String, MagicItem))) 
... 
Dim serializer As New DataContractSerializer(GetType(Dictionary(Of String, MagicItem)), KnownTypeList)