From 36d6b3a9e617fed91d3e9d6ac45abbfd339805d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 26 Jan 2021 19:34:40 +0100 Subject: [PATCH] Make XmlSerializer work with NativeAOT (#593) Force the reflection-only code paths that don't Reflection.Emit. --- .../BuildIntegration/Microsoft.NETCore.Native.targets | 3 +++ .../ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs | 2 +- .../System.Private.Xml/src/ILLink/ILLink.Substitutions.xml | 5 +++++ .../System.Private.Xml/src/System.Private.Xml.csproj | 3 +++ .../src/System/Xml/Serialization/XmlSerializer.cs | 6 ++++++ 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets index 72eeba3f9d53..0556bce6d6e0 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets @@ -228,6 +228,9 @@ The .NET Foundation licenses this file to you under the MIT license. + + + diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs index 46131d863ad6..57fc941b0761 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs @@ -876,7 +876,7 @@ protected override void ProcessField(FieldDesc field) private object TryCreateSubstitution(TypeDesc type, string value) { - switch (type.Category) + switch (type.UnderlyingType.Category) { case TypeFlags.Int32: if (string.IsNullOrEmpty(value)) diff --git a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml new file mode 100644 index 000000000000..f349d51ec47e --- /dev/null +++ b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj index 526cc0ea8461..9ee379d4b1fe 100644 --- a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj @@ -5,6 +5,9 @@ $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent) enable + + + diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs index 8c9b1d98859e..b316e88b87e3 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs @@ -199,6 +199,12 @@ public XmlSerializer(Type type, string? defaultNamespace) _primitiveType = type; return; } + + if (Mode == SerializationMode.ReflectionOnly) + { + return; + } + _tempAssembly = s_cache[defaultNamespace, type]; if (_tempAssembly == null) {