Use ReflectionOnly as serialization mode in case dynamic code runtime feature is...
authorMaxim Lipnin <v-maxlip@microsoft.com>
Tue, 3 Aug 2021 09:27:15 +0000 (12:27 +0300)
committerGitHub <noreply@github.com>
Tue, 3 Aug 2021 09:27:15 +0000 (12:27 +0300)
src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs
src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/Program.cs [new file with mode: 0644]
src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/iOS.Simulator.XmlSerializer_Deserialize.Test.csproj [new file with mode: 0644]

index 0498362..de4eb51 100644 (file)
@@ -9,6 +9,7 @@ using System.Diagnostics.CodeAnalysis;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
+using System.Runtime.CompilerServices;
 using System.Runtime.Versioning;
 using System.Security;
 using System.Text;
@@ -109,7 +110,13 @@ namespace System.Xml.Serialization
 
     public class XmlSerializer
     {
-        internal static SerializationMode Mode { get; set; } = SerializationMode.ReflectionAsBackup;
+        private static SerializationMode s_mode = SerializationMode.ReflectionAsBackup;
+
+        internal static SerializationMode Mode
+        {
+            get => RuntimeFeature.IsDynamicCodeSupported ? s_mode : SerializationMode.ReflectionOnly;
+            set => s_mode = value;
+        }
 
         private static bool ReflectionMethodEnabled
         {
diff --git a/src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/Program.cs b/src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/Program.cs
new file mode 100644 (file)
index 0000000..05da0ae
--- /dev/null
@@ -0,0 +1,43 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Threading.Tasks;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
+
+public static class Program
+{
+    public static async Task<int> Main(string[] args)
+    {
+        using StringReader stringReader = new StringReader(@"<?xml version=""1.0"" encoding=""UTF-8""?>
+            <TestClass>
+                <TestData>sample</TestData> 
+            </TestClass>");
+
+        var serializer = new XmlSerializer(typeof(TestClass));
+        TestClass obj = (TestClass)serializer.Deserialize(stringReader);
+
+        var result = obj.TestData == "sample" ? 42 : 1;
+
+        Console.WriteLine("Done!");
+        await Task.Delay(5000);
+
+        return result;
+    }
+
+    [XmlType("TestClass", AnonymousType = true, Namespace = "")]
+    public class TestClass
+    {
+        public TestClass()
+        {
+        }
+
+        [XmlElement("TestData")]
+        public string TestData { get; set; }
+    }
+}
diff --git a/src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/iOS.Simulator.XmlSerializer_Deserialize.Test.csproj b/src/tests/FunctionalTests/iOS/Simulator/XmlSerializer_Deserialize/iOS.Simulator.XmlSerializer_Deserialize.Test.csproj
new file mode 100644 (file)
index 0000000..e56efb0
--- /dev/null
@@ -0,0 +1,19 @@
+<Project Sdk="Microsoft.NET.Sdk" TreatAsLocalProperty="MonoForceInterpreter">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <MonoForceInterpreter>false</MonoForceInterpreter>
+    <RunAOTCompilation>true</RunAOTCompilation>
+    <TestRuntime>true</TestRuntime>
+    <TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
+    <TargetOS Condition="'$(TargetOS)' == ''">iOSSimulator</TargetOS>
+    <MainLibraryFileName>iOS.Simulator.XmlSerializer_Deserialize.Test.dll</MainLibraryFileName>
+    <IncludesTestRunner>false</IncludesTestRunner>
+    <ExpectedExitCode>42</ExpectedExitCode>
+    <EnableAggressiveTrimming>true</EnableAggressiveTrimming>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+  </ItemGroup>
+</Project>