Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielo-unity3d opened this issue Mar 20, 2024 · 3 comments

Comments

@danielo-unity3d
Copy link

danielo-unity3d commented Mar 20, 2024

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:

  • stripped of the current implementation, which adds a bunch of attributes, such as:
    [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 are instead simply made to inherit from IXmlSerializable (or some other scheme),

and then:

  • made partial, leaving the implementation or the read and write up to the dev, or
  • it's possible to completely generate the serialization code itself

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
Copy link
Owner

mganss commented Mar 25, 2024

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
Copy link

matshalf commented May 8, 2024

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
Copy link
Owner

mganss commented May 10, 2024

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?

@mganss mganss closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants