Skip to content

Commit 36d6b3a

Browse files
Make XmlSerializer work with NativeAOT (#593)
Force the reflection-only code paths that don't Reflection.Emit.
1 parent 6a774dc commit 36d6b3a

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ The .NET Foundation licenses this file to you under the MIT license.
228228
<IlcArg Include="@(_IlcRootedAssemblies->'--root:%(Identity)')" />
229229
<IlcArg Include="@(_IlcConditionallyRootedAssemblies->'--conditionalroot:%(Identity)')" />
230230

231+
<!-- The Emit-based XmlSerializer won't work -->
232+
<IlcArg Include="--feature:System.Xml.Serialization.XmlSerializer.IsReflectionOnly=true" />
233+
231234
<!-- Workaround for https://github.com/dotnet/corefx/issues/36723 -->
232235
<IlcArg Include="--removefeature:SerializationGuard" />
233236
</ItemGroup>

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/FeatureSwitchManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ protected override void ProcessField(FieldDesc field)
876876

877877
private object TryCreateSubstitution(TypeDesc type, string value)
878878
{
879-
switch (type.Category)
879+
switch (type.UnderlyingType.Category)
880880
{
881881
case TypeFlags.Int32:
882882
if (string.IsNullOrEmpty(value))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<linker>
2+
<type fullname="System.Xml.Serialization.XmlSerializer" featurevalue="true" feature="System.Xml.Serialization.XmlSerializer.IsReflectionOnly">
3+
<method signature="System.Xml.Serialization.SerializationMode get_Mode()" body="stub" value="1" />
4+
</type>
5+
</linker>

src/libraries/System.Private.Xml/src/System.Private.Xml.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)</TargetFrameworks>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
8+
<ItemGroup>
9+
<ILLinkSubstitutionsXmls Include="ILLink\ILLink.Substitutions.xml" />
10+
</ItemGroup>
811
<ItemGroup>
912
<Compile Include="$(CommonPath)System\Text\StringBuilderCache.cs" Link="Common\System\StringBuilderCache.cs" />
1013
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ public XmlSerializer(Type type, string? defaultNamespace)
199199
_primitiveType = type;
200200
return;
201201
}
202+
203+
if (Mode == SerializationMode.ReflectionOnly)
204+
{
205+
return;
206+
}
207+
202208
_tempAssembly = s_cache[defaultNamespace, type];
203209
if (_tempAssembly == null)
204210
{

0 commit comments

Comments
 (0)