Skip to content

Commit 4128643

Browse files
Addressed trim warnings for Sentry.Maui
1 parent cd1aafb commit 4128643

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
1+
using Sentry.Internal;
2+
13
namespace Sentry.Maui.Internal;
24

35
internal static class PageNavigationExtensions
46
{
5-
private static readonly PropertyInfo? DestinationPageProperty =
6-
typeof(NavigatedFromEventArgs).GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.NonPublic);
7+
private static readonly PropertyInfo? DestinationPageProperty;
8+
private static readonly PropertyInfo? PreviousPageProperty;
9+
10+
[UnconditionalSuppressMessage("Trimming", "IL2075: DynamicallyAccessedMembers", Justification = AotHelper.AvoidAtRuntime)]
11+
static PageNavigationExtensions()
12+
{
13+
if (AotHelper.IsTrimmed)
14+
{
15+
return;
16+
}
717

8-
private static readonly PropertyInfo? PreviousPageProperty =
9-
typeof(NavigatedToEventArgs).GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.NonPublic);
18+
var eventArgsType = typeof(NavigatedFromEventArgs);
19+
DestinationPageProperty =
20+
eventArgsType.GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.NonPublic);
21+
PreviousPageProperty =
22+
eventArgsType.GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.NonPublic);
23+
}
1024

25+
/// <summary>
26+
/// Reads the (internal) NavigatedFromEventArgs.DestinationPage property via reflection.
27+
/// Note that this will return null if trimming is enabled.
28+
/// </summary>
1129
public static Page? GetDestinationPage(this NavigatedFromEventArgs eventArgs) =>
1230
DestinationPageProperty?.GetValue(eventArgs) as Page;
1331

32+
/// <summary>
33+
/// Reads the (internal) NavigatedFromEventArgs.PreviousPage property via reflection.
34+
/// Note that this will return null if trimming is enabled.
35+
/// </summary>
1436
public static Page? GetPreviousPage(this NavigatedToEventArgs eventArgs) =>
1537
PreviousPageProperty?.GetValue(eventArgs) as Page;
1638
}

test/Sentry.TrimTest/Sentry.TrimTest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
<ItemGroup>
1515
<ProjectReference Include="..\..\src\Sentry\Sentry.csproj" />
16+
<ProjectReference Include="..\..\src\Sentry.Maui\Sentry.Maui.csproj" />
1617
<TrimmerRootAssembly Include="Sentry" />
18+
<TrimmerRootAssembly Include="Sentry.Maui" />
1719
</ItemGroup>
1820

1921
</Project>

0 commit comments

Comments
 (0)