Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 977d3eb

Browse files
authored
Fix several code generator issues. (#19737)
* Fix Sgen issues. * Updates based on the CR feedback. * Update the location of else if condition when type is TimeSpan * Update the namespace.
1 parent 486ad25 commit 977d3eb

40 files changed

+271
-185
lines changed

src/Microsoft.XmlSerializer.Generator/src/Microsoft.XmlSerializer.Generator.csproj

Lines changed: 6 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,12 @@
6666
<Compile Include="$(SourceDir)\System\Xml\Serialization\PrimitiveXmlSerializers.cs">
6767
<Link>System\Xml\Serialization\PrimitiveXmlSerializers.cs</Link>
6868
</Compile>
69-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapAttributeAttribute.cs">
70-
<Link>System\Xml\Serialization\SoapAttributeAttribute.cs</Link>
71-
</Compile>
72-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapAttributeOverrides.cs">
73-
<Link>System\Xml\Serialization\SoapAttributeOverrides.cs</Link>
74-
</Compile>
75-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapAttributes.cs">
76-
<Link>System\Xml\Serialization\SoapAttributes.cs</Link>
77-
</Compile>
78-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapElementAttribute.cs">
79-
<Link>System\Xml\Serialization\SoapElementAttribute.cs</Link>
80-
</Compile>
81-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapEnumAttribute.cs">
82-
<Link>System\Xml\Serialization\SoapEnumAttribute.cs</Link>
83-
</Compile>
84-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapIgnoreAttribute.cs">
85-
<Link>System\Xml\Serialization\SoapIgnoreAttribute.cs</Link>
86-
</Compile>
87-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapIncludeAttribute.cs">
88-
<Link>System\Xml\Serialization\SoapIncludeAttribute.cs</Link>
89-
</Compile>
9069
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapReflectionImporter.cs">
9170
<Link>System\Xml\Serialization\SoapReflectionImporter.cs</Link>
9271
</Compile>
9372
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapSchemamember.cs">
9473
<Link>System\Xml\Serialization\SoapSchemamember.cs</Link>
9574
</Compile>
96-
<Compile Include="$(SourceDir)\System\Xml\Serialization\SoapTypeAttribute.cs">
97-
<Link>System\Xml\Serialization\SoapTypeAttribute.cs</Link>
98-
</Compile>
9975
<Compile Include="$(SourceDir)\System\Xml\Serialization\TypeCode.cs">
10076
<Link>System\Xml\Serialization\TypeCode.cs</Link>
10177
</Compile>
@@ -105,57 +81,18 @@
10581
<Compile Include="$(SourceDir)\System\Xml\Serialization\Types.cs">
10682
<Link>System\Xml\Serialization\Types.cs</Link>
10783
</Compile>
108-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAnyAttributeAttribute.cs">
109-
<Link>System\Xml\Serialization\XmlAnyAttributeAttribute.cs</Link>
110-
</Compile>
111-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAnyElementAttribute.cs">
112-
<Link>System\Xml\Serialization\XmlAnyElementAttribute.cs</Link>
113-
</Compile>
114-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAnyElementAttributes.cs">
115-
<Link>System\Xml\Serialization\XmlAnyElementAttributes.cs</Link>
116-
</Compile>
117-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlArrayAttribute.cs">
118-
<Link>System\Xml\Serialization\XmlArrayAttribute.cs</Link>
119-
</Compile>
120-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlArrayItemAttribute.cs">
121-
<Link>System\Xml\Serialization\XmlArrayItemAttribute.cs</Link>
122-
</Compile>
123-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlArrayItemAttributes.cs">
124-
<Link>System\Xml\Serialization\XmlArrayItemAttributes.cs</Link>
125-
</Compile>
126-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAttributeAttribute.cs">
127-
<Link>System\Xml\Serialization\XmlAttributeAttribute.cs</Link>
128-
</Compile>
12984
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAttributeOverrides.cs">
13085
<Link>System\Xml\Serialization\XmlAttributeOverrides.cs</Link>
13186
</Compile>
13287
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlAttributes.cs">
13388
<Link>System\Xml\Serialization\XmlAttributes.cs</Link>
13489
</Compile>
135-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlChoiceIdentifierAttribute.cs">
136-
<Link>System\Xml\Serialization\XmlChoiceIdentifierAttribute.cs</Link>
137-
</Compile>
13890
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlCountingReader.cs">
13991
<Link>System\Xml\Serialization\XmlCountingReader.cs</Link>
14092
</Compile>
14193
<Compile Include="$(SourceDir)\System\Xml\Serialization\Xmlcustomformatter.cs">
14294
<Link>System\Xml\Serialization\Xmlcustomformatter.cs</Link>
14395
</Compile>
144-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlElementAttribute.cs">
145-
<Link>System\Xml\Serialization\XmlElementAttribute.cs</Link>
146-
</Compile>
147-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlElementAttributes.cs">
148-
<Link>System\Xml\Serialization\XmlElementAttributes.cs</Link>
149-
</Compile>
150-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlEnumAttribute.cs">
151-
<Link>System\Xml\Serialization\XmlEnumAttribute.cs</Link>
152-
</Compile>
153-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlIgnoreAttribute.cs">
154-
<Link>System\Xml\Serialization\XmlIgnoreAttribute.cs</Link>
155-
</Compile>
156-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlIncludeAttribute.cs">
157-
<Link>System\Xml\Serialization\XmlIncludeAttribute.cs</Link>
158-
</Compile>
15996
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlMapping.cs">
16097
<Link>System\Xml\Serialization\XmlMapping.cs</Link>
16198
</Compile>
@@ -165,18 +102,12 @@
165102
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlMembersMapping.cs">
166103
<Link>System\Xml\Serialization\XmlMembersMapping.cs</Link>
167104
</Compile>
168-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlNamespaceDeclarationsAttribute.cs">
169-
<Link>System\Xml\Serialization\XmlNamespaceDeclarationsAttribute.cs</Link>
170-
</Compile>
171105
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlReflectionImporter.cs">
172106
<Link>System\Xml\Serialization\XmlReflectionImporter.cs</Link>
173107
</Compile>
174108
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlReflectionMember.cs">
175109
<Link>System\Xml\Serialization\XmlReflectionMember.cs</Link>
176110
</Compile>
177-
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlRootAttribute.cs">
178-
<Link>System\Xml\Serialization\XmlRootAttribute.cs</Link>
179-
</Compile>
180111
<Compile Include="$(SourceDir)\System\Xml\Serialization\XmlSchemaProviderAttribute.cs">
181112
<Link>System\Xml\Serialization\XmlSchemaProviderAttribute.cs</Link>
182113
</Compile>
@@ -222,6 +153,12 @@
222153
<Compile Include="Microsoft\XmlSerializer\Generator\XmlSerializationReader.cs" />
223154
<Compile Include="Microsoft\XmlSerializer\Generator\XmlSerializationWriter.cs" />
224155
<Compile Include="Sgen.cs" />
156+
<Compile Include="System\Xml\Serialization\SoapAttributesExtensions.cs" />
157+
<Compile Include="System\Xml\Serialization\XmlAnyElementAttributeExtensions.cs" />
158+
<Compile Include="System\Xml\Serialization\XmlArrayItemAttributeExtensions.cs" />
159+
<Compile Include="System\Xml\Serialization\XmlChoiceIdentifierAttributeExtensions.cs" />
160+
<Compile Include="System\Xml\Serialization\XmlElementAttributeExtension.cs" />
161+
<Compile Include="System\Xml\Serialization\XmlRootAttributeExtensions.cs" />
225162
</ItemGroup>
226163
<ItemGroup>
227164
<Reference Include="System.Resources.ResourceManager" />

src/Microsoft.XmlSerializer.Generator/src/Microsoft/XmlSerializer/Generator/XmlSerializationWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,12 +2074,21 @@ private void WriteValue(object value)
20742074
Writer.Write(((DateTime)value).Ticks.ToString(CultureInfo.InvariantCulture));
20752075
Writer.Write(")");
20762076
}
2077+
else if (type == typeof(TimeSpan))
2078+
{
2079+
Writer.Write(" new ");
2080+
Writer.Write(type.FullName);
2081+
Writer.Write("(");
2082+
Writer.Write(((TimeSpan)value).Ticks.ToString(CultureInfo.InvariantCulture));
2083+
Writer.Write(")");
2084+
}
20772085
else
20782086
{
20792087
if (type.IsEnum)
20802088
{
20812089
Writer.Write(((int)value).ToString(null, NumberFormatInfo.InvariantInfo));
20822090
}
2091+
20832092
else
20842093
{
20852094
throw new InvalidOperationException(SR.Format(SR.XmlUnsupportedDefaultType, type.FullName));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class SoapAttributesExtensions
16+
{
17+
private static PropertyInfo s_soapFlagsPropertyInfo;
18+
internal static SoapAttributeFlags GetSoapFlags(this SoapAttributes soapAtt)
19+
{
20+
if (s_soapFlagsPropertyInfo == null)
21+
{
22+
s_soapFlagsPropertyInfo = typeof(SoapAttributes).GetProperty("SoapFlags", BindingFlags.NonPublic | BindingFlags.Instance);
23+
}
24+
25+
return (SoapAttributeFlags)s_soapFlagsPropertyInfo.GetValue(soapAtt);
26+
}
27+
}
28+
29+
internal enum SoapAttributeFlags
30+
{
31+
Enum = 0x1,
32+
Type = 0x2,
33+
Element = 0x4,
34+
Attribute = 0x8,
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class XmlAnyElementAttributeExtensions
16+
{
17+
private static PropertyInfo s_NamespaceSpecifiedPropertyInfo;
18+
internal static bool GetNamespaceSpecified(this XmlAnyElementAttribute xmlAnyElementAtt)
19+
{
20+
if(s_NamespaceSpecifiedPropertyInfo == null)
21+
{
22+
s_NamespaceSpecifiedPropertyInfo = typeof(XmlAnyElementAttribute).GetType().GetProperty("NamespaceSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
23+
}
24+
25+
return (bool)s_NamespaceSpecifiedPropertyInfo.GetValue(xmlAnyElementAtt);
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class XmlArrayItemAttributeExtensions
16+
{
17+
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
18+
internal static bool GetIsNullableSpecified(this XmlArrayItemAttribute xmlArrayItemAtt)
19+
{
20+
if(s_IsNullableSpecifiedPropertyInfo == null)
21+
{
22+
s_IsNullableSpecifiedPropertyInfo= typeof(XmlArrayItemAttribute).GetType().GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
23+
}
24+
25+
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlArrayItemAtt);
26+
}
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class XmlChoiceIdentifierAttributeExtensions
16+
{
17+
private static PropertyInfo s_MemberInfoPropertyInfo = typeof(XmlChoiceIdentifierAttribute).GetProperty("MemberInfo", BindingFlags.NonPublic | BindingFlags.Instance);
18+
19+
internal static MemberInfo GetMemberInfo(this XmlChoiceIdentifierAttribute xmlChoiceIdentifierAtt)
20+
{
21+
return (MemberInfo)s_MemberInfoPropertyInfo.GetValue(xmlChoiceIdentifierAtt);
22+
}
23+
24+
internal static void SetMemberInfo(this XmlChoiceIdentifierAttribute xmlChoiceIdentifierAtt, MemberInfo memberInfo)
25+
{
26+
s_MemberInfoPropertyInfo.SetValue(xmlChoiceIdentifierAtt, memberInfo);
27+
}
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class XmlElementAttributeExtension
16+
{
17+
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
18+
19+
internal static bool GetIsNullableSpecified(this XmlElementAttribute xmlElementAtt)
20+
{
21+
if(s_IsNullableSpecifiedPropertyInfo == null)
22+
{
23+
s_IsNullableSpecifiedPropertyInfo = typeof(XmlElementAttribute).GetType().GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
24+
}
25+
26+
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlElementAtt);
27+
}
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Reflection;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Xml.Serialization;
12+
13+
namespace Microsoft.XmlSerializer.Generator
14+
{
15+
internal static class XmlRootAttributeExtensions
16+
{
17+
private static PropertyInfo s_IsNullableSpecifiedPropertyInfo;
18+
private static PropertyInfo s_KeydPropertyInfo;
19+
20+
internal static bool GetIsNullableSpecified(this XmlRootAttribute xmlRootAtt)
21+
{
22+
if(s_IsNullableSpecifiedPropertyInfo == null)
23+
{
24+
s_IsNullableSpecifiedPropertyInfo = typeof(XmlRootAttribute).GetProperty("IsNullableSpecified", BindingFlags.NonPublic | BindingFlags.Instance);
25+
}
26+
27+
return (bool)s_IsNullableSpecifiedPropertyInfo.GetValue(xmlRootAtt);
28+
}
29+
30+
internal static string GetKey(this XmlRootAttribute xmlRootAtt)
31+
{
32+
if (s_KeydPropertyInfo == null)
33+
{
34+
s_KeydPropertyInfo = typeof(XmlRootAttribute).GetProperty("Key", BindingFlags.NonPublic | BindingFlags.Instance);
35+
}
36+
37+
return (string)s_KeydPropertyInfo.GetValue(xmlRootAtt);
38+
}
39+
}
40+
}

src/System.Private.Xml/src/System/Xml/Serialization/CodeGenerationoptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace System.Xml.Serialization
1010
{
1111
using System;
1212
using System.ComponentModel;
13+
using System.Xml.Serialization;
1314

1415
/// <include file='doc\CodeGenerationOptions.uex' path='docs/doc[@for="CodeGenerationOptions"]/*' />
1516
/// <devdoc>

src/System.Private.Xml/src/System/Xml/Serialization/IXmlSerializable.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
#if XMLSERIALIZERGENERATOR
6-
namespace Microsoft.XmlSerializer.Generator
7-
#else
85
namespace System.Xml.Serialization
9-
#endif
106
{
117
using System.Xml;
128
using System.Xml.Schema;

0 commit comments

Comments
 (0)