Skip to content

Commit

Permalink
Make XmlSerializer work with NativeAOT (dotnet#593)
Browse files Browse the repository at this point in the history
Force the reflection-only code paths that don't Reflection.Emit.
  • Loading branch information
MichalStrehovsky authored Jan 26, 2021
1 parent 6a774dc commit 36d6b3a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ The .NET Foundation licenses this file to you under the MIT license.
<IlcArg Include="@(_IlcRootedAssemblies->'--root:%(Identity)')" />
<IlcArg Include="@(_IlcConditionallyRootedAssemblies->'--conditionalroot:%(Identity)')" />

<!-- The Emit-based XmlSerializer won't work -->
<IlcArg Include="--feature:System.Xml.Serialization.XmlSerializer.IsReflectionOnly=true" />

<!-- Workaround for https://github.com/dotnet/corefx/issues/36723 -->
<IlcArg Include="--removefeature:SerializationGuard" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<linker>
<type fullname="System.Xml.Serialization.XmlSerializer" featurevalue="true" feature="System.Xml.Serialization.XmlSerializer.IsReflectionOnly">
<method signature="System.Xml.Serialization.SerializationMode get_Mode()" body="stub" value="1" />
</type>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ILLinkSubstitutionsXmls Include="ILLink\ILLink.Substitutions.xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonPath)System\Text\StringBuilderCache.cs" Link="Common\System\StringBuilderCache.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 36d6b3a

Please sign in to comment.