mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
626 stars 180 forks source link

Is it possible to generate serialization code which doesn't use reflection? #500

Closed danielo-unity3d closed 6 months ago

danielo-unity3d commented 8 months ago

We want the C# code generated from our XSD description (imported into the ModelBuilder) to be either trimmable (already possible?), or not need reflection at all (targetable from NativeAOT)?

My understanding is that a CodeDom is built from the XSD (the ModelBuilder).

So either, the generated classes are:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.732.0")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute("theClassName", Namespace="")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]

and then:

I know that we have access to the code and that anything is possible, but I'm looking for guidelines that would perhaps help accelerate the effort, or dissuade me from the undertaking. Has anyone ever asked for anything similar? Thanks

mganss commented 8 months ago

I don't understand the use case completely. The generated classes are intended to be used with XmlSerializer so if that's compatible with native AOT it should work. I don't think the generated classes themselves contain any code that requires reflection. The classes are already partial.

matshalf commented 6 months ago

I think what @danielo-unity3d means is an automatic implementation of the IXmlSerializable interface. When the XmlSerializer discovers that a class has implemented this interface the interface code is used rather than reflection based serialization. This can make the serialization and deserialization an order of magnitude faster.

mganss commented 6 months ago

No, an automatic implementation of IXmlSerializable is not possible in the current implementation and outside the scope of the project.

Have you considered using SGen?