none
Serialisierung von List of(... ) die von einem Interface abstammt RRS feed

  • Frage

  • Hallo

    Ich habe mir ein kleines Interface gebastelt:

    Public Interface IMainSettings
            Property Name As String
            <DefaultValueAttribute(TileType.Double)> _
            Property SizeType As TileType
            Property BackgroundColor As Color
            Property [Text] As String
            Property Index As Int32
            Enum TileType
                [Double]
                [Single]
                [Quater]
            End Enum
        End Interface

    In meinem Code verwende ich nun folgendes:

     <Serializable> _
        Public Class MainSetting
            Implements IntelliSoft.Interfaces.Settings.IMainSettings
    
            Public Property BackgroundColor As Color Implements IntelliSoft.Interfaces.Settings.IMainSettings.BackgroundColor
    
            Public Property Index As Integer Implements IntelliSoft.Interfaces.Settings.IMainSettings.Index
    
            Public Property Name As String Implements IntelliSoft.Interfaces.Settings.IMainSettings.Name
    
            Public Property SizeType As IntelliSoft.Interfaces.Settings.IMainSettings.TileType Implements IntelliSoft.Interfaces.Settings.IMainSettings.SizeType
    
            Public Property Text As String Implements IntelliSoft.Interfaces.Settings.IMainSettings.Text
    
        End Class

    dann versuche ich per XML serialisierung diese Daten in eine Datei zu speichern:

      Dim xmlser As New XmlSerializer(GetType(List(Of MainSetting)), _
                                                        New Type() {dataToSave.GetType()})
                    xmlser.Serialize(lStreamWriter, dataToSave)

    Doch dabei bekomme ich folgende Fehlermeldung:

    {System.InvalidOperationException: Fehler beim Reflektieren des Typs 'ISSettings.Core.MainSetting'. ---> System.NotSupportedException: Die Schnittstelle IntelliSoft.Interfaces.Settings.IMainSettings kann nicht serialisiert werden." & vbCrLf & "   bei System.Xml.Serialization.TypeDesc.CheckSupported()" & vbCrLf & "   bei System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)" & vbCrLf & "   bei System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference)" & vbCrLf & "   bei System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)" & vbCrLf & "   bei System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)" & vbCrLf & "   bei System.Xml.Serialization.StructModel.GetPropertyModel(PropertyInfo propertyInfo)" & vbCrLf & "   bei System.Xml.Serialization.StructModel.GetFieldModel(MemberInfo memberInfo)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)" & vbCrLf & "   --- Ende der internen Ausnahmestapelüberwachung ---" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, String arrayElementNs, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportArrayLikeMapping(ArrayModel model, String ns, RecursionLimiter limiter)" & vbCrLf & "   bei System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)}

    Wie kann ich das richtig machen?

    DANKE

    Sonntag, 12. Mai 2013 16:34

Antworten

  • Hi,
    nimm die Deklaration der Aufzählung raus aus der Deklaration des Interfaces. Dann klappt es.

    Nachfolgend eine Windows Forms Demo:

    Imports System.ComponentModel
    Imports System.Xml.Serialization
    
    Public Class Form1
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim dataToSave As New List(Of MainSetting)
        dataToSave.Add(New MainSetting With {.BackgroundColor = Color.Black, _
                                             .Index = 1, _
                                             .Name = "Name1", _
                                             .SizeType = TileType.Double, _
                                             .Text = "Name 1"})
    
        Using lStreamWriter As New IO.StreamWriter("c:\temp\x.x")
          Dim xmlser As New XmlSerializer(GetType(List(Of MainSetting)), New Type() {dataToSave.GetType()})
          xmlser.Serialize(lStreamWriter, dataToSave)
        End Using
    
      End Sub
    
    End Class
    
    <Serializable()> _
    Public Enum TileType
      [Double]
      [Single]
      [Quater]
    End Enum
    
    Public Interface IMainSettings
      Property Name As String
      <DefaultValueAttribute(TileType.Double)> _
      Property SizeType As TileType
      Property BackgroundColor As Color
      Property [Text] As String
      Property Index As Int32
    End Interface
    
    <Serializable> _
    Public Class MainSetting
      Implements IMainSettings
      Public Property BackgroundColor As Color Implements IMainSettings.BackgroundColor
      Public Property Index As Integer Implements IMainSettings.Index
      Public Property Name As String Implements IMainSettings.Name
      Public Property [Text] As String Implements IMainSettings.Text
      Public Property SizeType As TileType Implements IMainSettings.SizeType
    End Class
    

    --
    Viele Gruesse
    Peter

    Sonntag, 12. Mai 2013 17:26

Alle Antworten

  • Hallo, Schnittstellen kann man nicht so einfach Serialisieren. Diese müssen dazu ISerializable implementieren. Siehe auch:
    http://stackoverflow.com/questions/2639362/why-are-interfaces-not-serializable

    <Code:13/> - Koopakiller [kuːpakɪllɐ]
    Webseite | Code Beispiele | Facebook | Snippets
    Wenn die Frage beantwortet ist, dann markiert die hilfreichsten Beiträge als Antwort und bewertet die Beiträge. Danke.
    Einen Konverter zwischen C# und VB.NET Code gibt es hier.

    Sonntag, 12. Mai 2013 16:53
    Moderator
  • Hi,
    nimm die Deklaration der Aufzählung raus aus der Deklaration des Interfaces. Dann klappt es.

    Nachfolgend eine Windows Forms Demo:

    Imports System.ComponentModel
    Imports System.Xml.Serialization
    
    Public Class Form1
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim dataToSave As New List(Of MainSetting)
        dataToSave.Add(New MainSetting With {.BackgroundColor = Color.Black, _
                                             .Index = 1, _
                                             .Name = "Name1", _
                                             .SizeType = TileType.Double, _
                                             .Text = "Name 1"})
    
        Using lStreamWriter As New IO.StreamWriter("c:\temp\x.x")
          Dim xmlser As New XmlSerializer(GetType(List(Of MainSetting)), New Type() {dataToSave.GetType()})
          xmlser.Serialize(lStreamWriter, dataToSave)
        End Using
    
      End Sub
    
    End Class
    
    <Serializable()> _
    Public Enum TileType
      [Double]
      [Single]
      [Quater]
    End Enum
    
    Public Interface IMainSettings
      Property Name As String
      <DefaultValueAttribute(TileType.Double)> _
      Property SizeType As TileType
      Property BackgroundColor As Color
      Property [Text] As String
      Property Index As Int32
    End Interface
    
    <Serializable> _
    Public Class MainSetting
      Implements IMainSettings
      Public Property BackgroundColor As Color Implements IMainSettings.BackgroundColor
      Public Property Index As Integer Implements IMainSettings.Index
      Public Property Name As String Implements IMainSettings.Name
      Public Property [Text] As String Implements IMainSettings.Text
      Public Property SizeType As TileType Implements IMainSettings.SizeType
    End Class
    

    --
    Viele Gruesse
    Peter

    Sonntag, 12. Mai 2013 17:26
  • Danke für die Lösung!  - Das Auslagern der ENUM's hat das Problem gelöst!

    Entschuldige die späte Antwort!

    Mittwoch, 15. Mai 2013 17:18