none
Cannot serialize class inside interface RRS feed

  • Question

  • My code in a Portable library project:

    Public Interface IMyInterface
        Class MyClass
            Property MiaProp1 As Integer
            Property MiaProp2 As Integer
        End Class
        ... (Functions, etc.)
    End Interface
    
    Dim Ser As New XmlSerializer(GetType(IMyInterface.MyClass))
    This gives error saying the interface (not the class that I want to use, but the external interface) cannot be serialized.
    I cannot move the class outside the interface. Is there another way to make it work?
    Tuesday, February 28, 2017 5:43 PM

Answers

  • Ironically, the same code line (along with the same class in an interface) works perfectly in Android projects:

    Android and .NET are two different platforms. 

    So I made many "Class in an Interface", and I should do a lot of changes in my portable projects.

    Did you do it with .NET languages?

    The interface should behave like a namespace in my case.

    https://msdn.microsoft.com/en-us/library/87d83y5b.aspx

    <copied>

    An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

    <end>

    You can't make up your own rules as to what you think an Interface being used by a .NET language is supposed to be doing. Interfaces don't act like a namespace in .NET.

    Is there any workaround to obtain this? Some attribute or similar?

    With what you are trying to do? I doubt it.

    Wednesday, March 1, 2017 3:19 PM

All replies

  • You can't serialize an interface and not a class inside the interface either. Normally interfaces do not contain classes. I have never seen a class in an interface. What you are trying to do seems questionable.
    Tuesday, February 28, 2017 6:12 PM
  • Ironically, the same code line (along with the same class in an interface) works perfectly in Android projects:

    XmlSerializer Ser = new XmlSerializer(typeof(IMyInterface.MyClass));

    (some sort of bug?)

    So I made many "Class in an Interface", and I should do a lot of changes in my portable projects.

    The interface should behave like a namespace in my case.

    Is there any workaround to obtain this? Some attribute or similar?


    Wednesday, March 1, 2017 8:43 AM
  • Ironically, the same code line (along with the same class in an interface) works perfectly in Android projects:

    Android and .NET are two different platforms. 

    So I made many "Class in an Interface", and I should do a lot of changes in my portable projects.

    Did you do it with .NET languages?

    The interface should behave like a namespace in my case.

    https://msdn.microsoft.com/en-us/library/87d83y5b.aspx

    <copied>

    An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

    <end>

    You can't make up your own rules as to what you think an Interface being used by a .NET language is supposed to be doing. Interfaces don't act like a namespace in .NET.

    Is there any workaround to obtain this? Some attribute or similar?

    With what you are trying to do? I doubt it.

    Wednesday, March 1, 2017 3:19 PM
  • Did you do it with .NET languages?

    Yes, I do. Interfaces and other stuff in Portable Projects with vb.net, UI in Android projects (Xamarin Forms) with C#.

    I'm going to make my changes now, removing classes from interfaces.

    Thanks for your help and explanations!
    Wednesday, March 1, 2017 4:36 PM