-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[trimming] Enable
$(_DefaultValueAttributeSupport)
for `$(TrimMode)…
…=partial` (#9525) Context: dotnet/runtime#109724 In .NET 9, certain apps could crash with: System.ArgumentException: RuntimeInstanceNotAllowed ?, in object DefaultValueAttribute.get_Value() ?, in new XmlAttributes(ICustomAttributeProvider) ?, in XmlAttributes XmlReflectionImporter.GetAttributes(MemberInfo) ?, in bool XmlReflectionImporter.InitializeStructMembers(StructMapping, StructModel, bool, string, RecursionLimiter) ?, in StructMapping XmlReflectionImporter.ImportStructLikeMapping(StructModel, string, bool, XmlAttributes, RecursionLimiter) .NET's concept of `$(PublishTrimmed)` is used to decide which trimmer feature switches are toggled. This is normally set for both Debug & Release, but Android only sets it for Release. This means that the `$(_DefaultValueAttributeSupport)` feature switch is not set properly in some cases, which causes the crash. For now, we can set `$(_DefaultValueAttributeSupport)` for `TrimMode=partial`, the default in .NET MAUI apps. See #9526 for how we might better address this in the future. In order to test this change: * Add a `XmlSerializerTest` to `Mono.Android-Tests` * Run a copy of `Mono.Android-Tests` with `-p:TestsFlavor=TrimModePartial` * Also set `$(_DefaultValueAttributeSupport)` for `TrimMode=full` in our test project, so `XmlSerializerTest` will pass in that mode as well. Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
- Loading branch information
1 parent
84a95ef
commit 20f08bd
Showing
5 changed files
with
38 additions
and
1 deletion.
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
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
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,23 @@ | ||
using System.Xml.Serialization; | ||
using System.ComponentModel; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace System.XmlTests { | ||
public class C { | ||
[DefaultValue(typeof(C), "c")] | ||
public Type? T { get; } | ||
} | ||
|
||
[TestFixture] | ||
public class XmlSerializerTest { | ||
|
||
[Test] | ||
public void TrimmingDefaultValueAttribute () | ||
{ | ||
// Context: https://github.com/dotnet/runtime/issues/109724 | ||
var s = new XmlSerializer(typeof(C)); | ||
_ = new C().T; // Prevent C.T from being removed by trimming | ||
} | ||
} | ||
} |