0

I try to serialize and deserialize an object in C# but I don't like any of the solutions present in the world. What I have is a multi-level object which has interface properties what I do not want to change to concrete classes. Here is an example code:

public class Car
{
   // Yes, this must be private set!
   public string Manufacturer { get; private set; }

   public IEngine Engine { get; }
}

public interface IEngine
{
    public IModul MainModul { get; }
    public List<IModul> AdditionalModules { get; } 
}

public interface IModul
{
    public string ModulName{ get; }
    public Guid ModulId { get; }
}

I tried many approaches and all of them needs to have a lot of unnecessary/ugly boilerplate code. I want a clean and typeless xml to be generated therefore this example is not suitable: https://social.msdn.microsoft.com/Forums/en-US/bac96f79-82cd-4fef-a748-2a85370a8510/xmlserialization-with-interfaces?forum=asmxandxml

I also don't want to have multiple copies of my properties: XML serialization of interface property

I wanted to use something like this, but does not work at all:

public class Car
{
   public string Manufacturer { get; private set; }
   
   // Still getting the exception that Interfaces cannot be serialized
   // Inside the IEngine interface I used [XMLElement] attribute as well, but didn't help
   [XmlElement(Type = typeof(Engine))]
   public IEngine Engine { get; }
}

There is really no way in the world to serialize/deserialize an Interface by giving the ConcreteType as an attribute? Currently I'm using only .Net Core Json serializer and XmlSerializer from the same framework, therefor I do not want to use "BuddyXML" (just an example) or any other random framework.

Anyone having any idea?

  • This is a long-time pain of XmlSerializer. You have to use a workaround. There's an open-source library ExtendedXmlSerializer, which aims to fix these shortcomings. However, it has a few dependencies; for me the reason for not using it and sticking with workarounds. – Ondrej Tucny Jul 10 '22 at 10:39
  • System `XmlSerializer` does not support polymorphism on purpose (they did the same with the JSON serializer, too). If you need such features you must use other serializers, like [this one](https://github.com/koszeggy/KGySoft.CoreLibraries#xml-serialization) (disclaimer: written by me). And then you can use interfaces with those get-only properties as you can see in [this example](https://dotnetfiddle.net/gHfj2V). – György Kőszeg Jul 10 '22 at 10:54
  • Thanks for closing this question, I think someone misunderstood my problem. The linked question which is unfortunately does not help just, how to say it... does not help. I need better workarounds, I may open a discussion topic I guess. – Behold The Light Jul 10 '22 at 17:30
  • @GyörgyKőszeg Thank you for the library, sadly it does not suit my expectations. Having the type as an Attribute is not the way I want to handle data. I want to let the serializer to use exactly that concrete type I set for my property. Like: "[XMLType = typeof(V8Engine)]" above my IEngine property. I want it as loosely coupled as possible. – Behold The Light Jul 10 '22 at 17:34
  • @BeholdTheLight: Annotating the C# property with the possible actual types is quite a strong coupling IMHO. Which actually can be good for security reasons (no unexpected implementer is allowed) but it's very inflexible. _"Having the type as an Attribute is not the way I want to handle data"_ - the `type` attributes in the XML result describe the concrete serialized instance and are resolved on-the-fly on deserialization so it can be any type that implements the base type or interface. You need to consider the pros and cons and the capabilities of the possible solutions. – György Kőszeg Jul 10 '22 at 18:11

0 Answers0