Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ReflectionOnly as serialization mode in case dynamic code runtime feature is not supported #56604

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Text;
Expand Down Expand Up @@ -109,7 +110,7 @@ internal enum SerializationMode

public class XmlSerializer
{
internal static SerializationMode Mode { get; set; } = SerializationMode.ReflectionAsBackup;
internal static SerializationMode Mode { get; set; } = RuntimeFeature.IsDynamicCodeSupported ? SerializationMode.ReflectionAsBackup : SerializationMode.ReflectionOnly;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static SerializationMode Mode { get; set; } = RuntimeFeature.IsDynamicCodeSupported ? SerializationMode.ReflectionAsBackup : SerializationMode.ReflectionOnly;
internal static SerializationMode Mode => RuntimeFeature.IsDynamicCodeSupported ? SerializationMode.ReflectionAsBackup : SerializationMode.ReflectionOnly;

Is it possible to return the constant directly instead of caching it in a static variable, to enable linker to trim the unreachable code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those even used? Where are these compilation constants set?

#if ReflectionOnly|| XMLSERIALIZERGENERATORTESTS

cc: @StephenMolloy

Copy link
Member

@stephentoub stephentoub Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nevermind, there's a separate project that sets at least one of them:

It'd be good at some point to find a way to test this while not negatively impacting the size of what we ship.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, at this stage of the game, I'd stick with the original proposed change that caches the value at initialization so we maintain the ability to override when needed. (Which I believe is pretty much just testing right now.) We have a desire to potentially make the reflection-based serializer the primary option in the future, but we've got work to do to bring it up to par with the ILGen serializer before we can do that. Having a switch that is not stuck in one position is helpful.


private static bool ReflectionMethodEnabled
{
Expand Down
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;
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
{
[DllImport("__Internal")]
public static extern void mono_ios_set_summary (string value);

public static async Task<int> Main(string[] args)
{
mono_ios_set_summary($"Starting functional test");
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved

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; }
}
}
Original file line number Diff line number Diff line change
@@ -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>