Serialize types with same name but different namespace

338 views Asked by At

I´m using SGen to create XmlSerilaization-assemblies for the types within my assembly. The following is a simplified code-example of this assembly:

namespace ConsoleApplication1
{
    public class Program
    {
        public MyNS2.Flurstueck Flurstueck = new MyNS2.Flurstueck();
    }
}

namespace ConsoleApplication1.MyNS1
{
    [XmlRoot("Flurstueck1")]
    public class Flurstueck
    {
        public string kz1 = "kz1";
    }
}
namespace ConsoleApplication1.MyNS2
{
    [XmlRoot("Flurstueck2")]
    public class Flurstueck
    {
        public string kz2 = "kz2";
    }
}

When I compile using and enable the creation of serializer-assemblies for that project I get "error on reflecting type ConsoleApplication1.MySN1.Flurstueck". Obviously we have a naming-conflict here because the classes MyNS1.Flurstueck and MyNS2.Flurstueck have the same name. This of course compiles as both types have different namespaces. When creating the serializer-assembly it fails.

However when I manually serilize that type into a file and deserialize it again (and thus without creating the serializer-assembly and let the serialization happen in-time), it works:

XmlSerializer ser = new XmlSerializer(typeof(Program));
using (TextWriter writer = new StreamWriter(path))
{
    ser.Serialize(writer, new Program());
}

using (TextReader reader = new StreamReader(path))
{
    var o = ser.Deserialize(reader);
}

How can I get this to work with Sgen? Of course I could rename the types to be distinct, but I wonder why Sgen cannot resolve the types while XmlSerializer can.

0

There are 0 answers