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

Commit 977d580

Browse files
committed
Update the tests, fix typos in comments, and address review comments
1 parent 00ec7f6 commit 977d580

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,3 +2109,13 @@ public class TypeWithXmlDocumentProperty
21092109
{
21102110
public XmlDocument Document;
21112111
}
2112+
2113+
public class TypeWithNonParameterlessConstructor
2114+
{
2115+
public string StringProperty { get; set; }
2116+
2117+
public TypeWithNonParameterlessConstructor(string value)
2118+
{
2119+
StringProperty = value;
2120+
}
2121+
}

src/System.Xml.XmlSerializer/src/System/Xml/Serialization/XmlSerializationWriterILGen.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,8 +2419,8 @@ internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bo
24192419
Label labelReturn = ilg.DefineLabel();
24202420
Label labelEndIf = ilg.DefineLabel();
24212421

2422-
// Type typeInfo = type.GetTypeInfo();
2423-
LocalBuilder typeInfo = ilg.DeclareLocal(typeof(TypeInfo), "typeInfo");
2422+
// TypeInfo typeInfo = type.GetTypeInfo();
2423+
// typeInfo not declared explicitly
24242424
ilg.Ldc(type);
24252425
MethodInfo getTypeInfoMehod = typeof(IntrospectionExtensions).GetMethod(
24262426
"GetTypeInfo",
@@ -2429,11 +2429,10 @@ internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bo
24292429
);
24302430
ilg.Call(getTypeInfoMehod);
24312431

2432-
// IEnumerator e = typeInfo.DeclaredConstructors.GetEnumerator();
2433-
LocalBuilder enumerator = ilg.DeclareLocal(typeof(IEnumerator), "e");
2432+
// IEnumerator<ConstructorInfo> e = typeInfo.DeclaredConstructors.GetEnumerator();
2433+
LocalBuilder enumerator = ilg.DeclareLocal(typeof(IEnumerator<>).MakeGenericType(typeof(ConstructorInfo)), "e");
24342434
MethodInfo getDeclaredConstructors = typeof(TypeInfo).GetMethod("get_DeclaredConstructors");
2435-
MethodInfo getEnumerator =
2436-
typeof(IEnumerable<>).MakeGenericType(typeof(ConstructorInfo)).GetMethod("GetEnumerator");
2435+
MethodInfo getEnumerator = typeof(IEnumerable<>).MakeGenericType(typeof(ConstructorInfo)).GetMethod("GetEnumerator");
24372436
ilg.Call(getDeclaredConstructors);
24382437
ilg.Call(getEnumerator);
24392438
ilg.Stloc(enumerator);
@@ -2446,7 +2445,7 @@ internal void ILGenForCreateInstance(CodeGenerator ilg, Type type, Type cast, bo
24462445
LocalBuilder constructorInfo = ilg.DeclareLocal(typeof(ConstructorInfo), "constructorInfo");
24472446
ilg.Stloc(constructorInfo);
24482447

2449-
// if (!constructorInfo.IsStatic || constructorInfo.GetParameters.Length() == 0)
2448+
// if (!constructorInfo.IsStatic && constructorInfo.GetParameters.Length() == 0)
24502449
ilg.Ldloc(constructorInfo);
24512450
MethodInfo constructorIsStatic = typeof(ConstructorInfo).GetMethod("get_IsStatic");
24522451
ilg.Call(constructorIsStatic);

src/System.Xml.XmlSerializer/tests/XmlSerializerTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Globalization;
99
using System.IO;
1010
using System.Linq;
11+
using System.Reflection;
1112
using System.Xml;
1213
using System.Xml.Serialization;
1314
using System.Xml.Linq;
@@ -1114,14 +1115,21 @@ public static void Xml_TypeWithMismatchBetweenAttributeAndPropertyType()
11141115
[Fact]
11151116
public static void Xml_TypeWithNonPublicDefaultConstructor()
11161117
{
1117-
System.Reflection.TypeInfo ti = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(TypeWithNonPublicDefaultConstructor));
1118+
TypeInfo ti = IntrospectionExtensions.GetTypeInfo(typeof(TypeWithNonPublicDefaultConstructor));
11181119
TypeWithNonPublicDefaultConstructor value = null;
11191120
value = (TypeWithNonPublicDefaultConstructor)FindDefaultConstructor(ti).Invoke(null);
11201121
Assert.StrictEqual("Mr. FooName", value.Name);
1121-
var actual = SerializeAndDeserialize<TypeWithNonPublicDefaultConstructor>(value, "<?xml version=\"1.0\"?>\r\n<TypeWithNonPublicDefaultConstructor xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <Name>Mr. FooName</Name>\r\n</TypeWithNonPublicDefaultConstructor>");
1122+
var actual = SerializeAndDeserialize(value, "<?xml version=\"1.0\"?>" + Environment.NewLine + "<TypeWithNonPublicDefaultConstructor xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + Environment.NewLine + " <Name>Mr. FooName</Name>" + Environment.NewLine + "</TypeWithNonPublicDefaultConstructor>");
11221123
Assert.StrictEqual(value.Name, actual.Name);
11231124
}
11241125

1126+
[Fact]
1127+
public static void Xml_TypeWithNonParameterlessConstructor()
1128+
{
1129+
var obj = new TypeWithNonParameterlessConstructor("string value");
1130+
Assert.Throws<InvalidOperationException>(() => { SerializeAndDeserialize(obj, string.Empty); });
1131+
}
1132+
11251133
private static T SerializeAndDeserialize<T>(T value, string baseline, Func<XmlSerializer> serializerFactory = null)
11261134
{
11271135
XmlSerializer serializer = new XmlSerializer(typeof(T));

0 commit comments

Comments
 (0)