XmlSchemaClassGenerator icon indicating copy to clipboard operation
XmlSchemaClassGenerator copied to clipboard

Generate classes without DataAnnotations

Open IceTrooper opened this issue 3 years ago • 5 comments

Is it possible to generate C# classes without the DataAnnotations? I'm using the generated classes inside Unity and I got an error: Assets\Scripts\Integrations\OpenDrive\XsdSchemas\out\Integrations.OpenDRIVE.cs(4987,48): error CS1069: The type name 'RequiredAttributeAttribute' could not be found in the namespace 'System.ComponentModel.DataAnnotations'. This type has been forwarded to assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Consider adding a reference to that assembly. and I can't add that reference to the project (I won't be even using those features).

Thank you :)

IceTrooper avatar Mar 25 '22 13:03 IceTrooper

This is currently not possible. What does the generated code look like? RequiredAttributeAttribute sounds strange, it should be RequiredAttribute.

mganss avatar Mar 25 '22 18:03 mganss

Sorry for the delayed reply.

The code looks like this:

/// <summary>
/// <para>s-coordinate of start position</para>
/// <para xml:lang="en">Minimum inclusive value: 0.0.</para>
/// </summary>
[System.ComponentModel.DescriptionAttribute("s-coordinate of start position")]
//[System.ComponentModel.DataAnnotations.RequiredAttribute()]
[System.Xml.Serialization.XmlAttributeAttribute("s")]
public double S { get; set; }

/// <summary>
/// <para>Start position (x inertial)</para>
/// </summary>
[System.ComponentModel.DescriptionAttribute("Start position (x inertial)")]
//[System.ComponentModel.DataAnnotations.RequiredAttribute()]
[System.Xml.Serialization.XmlAttributeAttribute("x")]
public double X { get; set; }

/// <summary>
/// <para>Start position (y inertial)</para>
/// </summary>
[System.ComponentModel.DescriptionAttribute("Start position (y inertial)")]
//[System.ComponentModel.DataAnnotations.RequiredAttribute()]
[System.Xml.Serialization.XmlAttributeAttribute("y")]
public double Y { get; set; }

Because of that, I had to manually comment out all of the occurrences of DataAnnotations, which is doable with Find and Replace, but tedious and I think it's not the right way of doing this. :/

This is the screenshot of the problem inside Visual Studio. obraz

I know that in an ordinary .NET application, you just need to add a reference to the appropriate assembly. However, in Unity it is not so trivial nor currently desired.

IceTrooper avatar Mar 28 '22 21:03 IceTrooper

OK I'll try and make this configurable. I won't be getting to it until next week, though.

mganss avatar Mar 31 '22 07:03 mganss

That's awesome to hear! The project where I'm using this tool will still be a while away, so it's no problem to wait for a little. Plus I think anyone using this tool on Unity projects would appreciate that configuration option.

As I've read on many forums these DataAnnotations due to being in a different .dll than the rest are a pain in the ass (even outside of Unity projects).

IceTrooper avatar Mar 31 '22 10:03 IceTrooper

There already is support for this feature through the -c option (PCL compatible output). This does a few other things as well but hopefully those won't impact your use case.

mganss avatar Apr 04 '22 16:04 mganss