Skip to content

Commit b3ab2eb

Browse files
authored
Use ReflectionOnly as serialization mode in case dynamic code runtime feature is not supported (#56604)
1 parent 7f931b1 commit b3ab2eb

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Globalization;
1010
using System.IO;
1111
using System.Reflection;
12+
using System.Runtime.CompilerServices;
1213
using System.Runtime.Versioning;
1314
using System.Security;
1415
using System.Text;
@@ -109,7 +110,13 @@ internal enum SerializationMode
109110

110111
public class XmlSerializer
111112
{
112-
internal static SerializationMode Mode { get; set; } = SerializationMode.ReflectionAsBackup;
113+
private static SerializationMode s_mode = SerializationMode.ReflectionAsBackup;
114+
115+
internal static SerializationMode Mode
116+
{
117+
get => RuntimeFeature.IsDynamicCodeSupported ? s_mode : SerializationMode.ReflectionOnly;
118+
set => s_mode = value;
119+
}
113120

114121
private static bool ReflectionMethodEnabled
115122
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Runtime.InteropServices;
8+
using System.Threading.Tasks;
9+
using System.Xml;
10+
using System.Xml.Schema;
11+
using System.Xml.Serialization;
12+
13+
public static class Program
14+
{
15+
public static async Task<int> Main(string[] args)
16+
{
17+
using StringReader stringReader = new StringReader(@"<?xml version=""1.0"" encoding=""UTF-8""?>
18+
<TestClass>
19+
<TestData>sample</TestData>
20+
</TestClass>");
21+
22+
var serializer = new XmlSerializer(typeof(TestClass));
23+
TestClass obj = (TestClass)serializer.Deserialize(stringReader);
24+
25+
var result = obj.TestData == "sample" ? 42 : 1;
26+
27+
Console.WriteLine("Done!");
28+
await Task.Delay(5000);
29+
30+
return result;
31+
}
32+
33+
[XmlType("TestClass", AnonymousType = true, Namespace = "")]
34+
public class TestClass
35+
{
36+
public TestClass()
37+
{
38+
}
39+
40+
[XmlElement("TestData")]
41+
public string TestData { get; set; }
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk" TreatAsLocalProperty="MonoForceInterpreter">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<MonoForceInterpreter>false</MonoForceInterpreter>
6+
<RunAOTCompilation>true</RunAOTCompilation>
7+
<TestRuntime>true</TestRuntime>
8+
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
9+
<TargetOS Condition="'$(TargetOS)' == ''">iOSSimulator</TargetOS>
10+
<MainLibraryFileName>iOS.Simulator.XmlSerializer_Deserialize.Test.dll</MainLibraryFileName>
11+
<IncludesTestRunner>false</IncludesTestRunner>
12+
<ExpectedExitCode>42</ExpectedExitCode>
13+
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<Compile Include="Program.cs" />
18+
</ItemGroup>
19+
</Project>

0 commit comments

Comments
 (0)