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?