-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XmlSerializer support for IsDynamicCodeSupported=false (#59386)
* XmlSerializer support for IsDynamicCodeSupported=false Add more checks to XmlSerializer to check the SerializationMode. Don't try to use Reflection.Emit if the SerializationMode is ReflectionOnly. These changes were ported from * dotnet/runtimelab#593 * dotnet/runtimelab#600 Fix #59167 * Fix a bug in XmlSerializer.CanDeserialize when in ReflectionOnly mode. * Port UAP code for CanDeserialize * PR feedback * Add a linker test to ensure linker option '--enable-opt sealer' works when IsDynamicCodeSupported==false.
- Loading branch information
Showing
3 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/libraries/System.Private.Xml/tests/TrimmingTests/XmlSerializer.Deserialize.SealerOpt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace System.Xml.Serialization.TrimmingTests | ||
{ | ||
/// <summary> | ||
/// Tests that using XmlSerializer with linker option '--enable-opt sealer' works | ||
/// when IsDynamicCodeSupported==false. | ||
/// </summary> | ||
internal class Program | ||
{ | ||
// Preserve these types until XmlSerializer is fully trim-safe. | ||
// see https://github.com/dotnet/runtime/issues/44768 | ||
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Response))] | ||
public static int Main() | ||
{ | ||
// simulate IsDynamicCodeSupported==false by setting the SerializationMode to ReflectionOnly | ||
const int ReflectionOnly = 1; | ||
typeof(XmlSerializer).GetField("s_mode", BindingFlags.NonPublic | BindingFlags.Static) | ||
.SetValue(null, ReflectionOnly); | ||
|
||
using StringReader stringReader = new StringReader(@"<?xml version=""1.0"" encoding=""UTF-8""?> | ||
<Response DataType=""Data""> | ||
</Response>"); | ||
|
||
Response obj = (Response)new XmlSerializer(typeof(Response)).Deserialize(stringReader); | ||
if (obj.DataType == "Data") | ||
{ | ||
return 100; | ||
} | ||
|
||
return -1; | ||
} | ||
} | ||
|
||
[Serializable] | ||
[XmlType(AnonymousType = true)] | ||
[XmlRoot(Namespace = "", IsNullable = false)] | ||
public class Response | ||
{ | ||
[XmlAttribute] | ||
public string DataType { get; set; } | ||
} | ||
} |