Skip to content

Add AOT reflection warnings for .NET 6+ methods #4085

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

Merged
merged 3 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/ReactiveUI/Expression/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ public static bool IsStatic(this PropertyInfo item) // TODO: Create Test
return method.IsStatic;
}

#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
internal static IObservable<object> ViewModelWhenAnyValue<TView, TViewModel>(TViewModel? viewModel, TView view, Expression? expression)
where TView : class, IViewFor
where TViewModel : class =>
Expand Down
4 changes: 4 additions & 0 deletions src/ReactiveUI/Interfaces/ISuspensionDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ public interface ISuspensionDriver
/// Invalidates the application state (i.e. deletes it from disk).
/// </summary>
/// <returns>A completed observable.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
IObservable<Unit> InvalidateState();
}
28 changes: 26 additions & 2 deletions src/ReactiveUI/Mixins/AutoPersistHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static class AutoPersistHelper
{
private static readonly MemoizingMRUCache<Type, Dictionary<string, bool>> _persistablePropertiesCache = new(
static (type, _) => type.GetTypeInfo().DeclaredProperties
.Where(x => x.CustomAttributes.Any(y => typeof(DataMemberAttribute).GetTypeInfo().IsAssignableFrom(y.AttributeType.GetTypeInfo())))
.ToDictionary(k => k.Name, _ => true),
.Where(x => x.CustomAttributes.Any(y => typeof(DataMemberAttribute).GetTypeInfo().IsAssignableFrom(y.AttributeType.GetTypeInfo())))
.ToDictionary(k => k.Name, _ => true),
RxApp.SmallCacheLimit);

private static readonly MemoizingMRUCache<Type, bool> _dataContractCheckCache = new(
Expand All @@ -49,6 +49,10 @@ public static class AutoPersistHelper
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersist<T>(this T @this, Func<T, IObservable<Unit>> doPersist, TimeSpan? interval = null)
where T : IReactiveObject =>
@this.AutoPersist(doPersist, Observable<Unit>.Never, interval);
Expand Down Expand Up @@ -76,6 +80,10 @@ public static IDisposable AutoPersist<T>(this T @this, Func<T, IObservable<Unit>
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersist<T, TDontCare>(this T @this, Func<T, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null)
where T : IReactiveObject
{
Expand Down Expand Up @@ -127,6 +135,10 @@ public static IDisposable AutoPersist<T, TDontCare>(this T @this, Func<T, IObser
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersistCollection<TItem>(this ObservableCollection<TItem> @this, Func<TItem, IObservable<Unit>> doPersist, TimeSpan? interval = null) // TODO: Create Test
where TItem : IReactiveObject =>
AutoPersistCollection(@this, doPersist, Observable<Unit>.Never, interval);
Expand All @@ -151,6 +163,10 @@ public static IDisposable AutoPersistCollection<TItem>(this ObservableCollection
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersistCollection<TItem, TDontCare>(this ObservableCollection<TItem> @this, Func<TItem, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null)
where TItem : IReactiveObject =>
AutoPersistCollection<TItem, ObservableCollection<TItem>, TDontCare>(@this, doPersist, manualSaveSignal, interval);
Expand All @@ -175,6 +191,10 @@ public static IDisposable AutoPersistCollection<TItem, TDontCare>(this Observabl
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersistCollection<TItem, TDontCare>(this ReadOnlyObservableCollection<TItem> @this, Func<TItem, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null) // TODO: Create Test
where TItem : IReactiveObject =>
AutoPersistCollection<TItem, ReadOnlyObservableCollection<TItem>, TDontCare>(@this, doPersist, manualSaveSignal, interval);
Expand All @@ -200,6 +220,10 @@ public static IDisposable AutoPersistCollection<TItem, TDontCare>(this ReadOnlyO
/// it is possible that it will never be saved.
/// </param>
/// <returns>A Disposable to disable automatic persistence.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static IDisposable AutoPersistCollection<TItem, TCollection, TDontCare>(this TCollection @this, Func<TItem, IObservable<Unit>> doPersist, IObservable<TDontCare> manualSaveSignal, TimeSpan? interval = null) // TODO: Create Test
where TItem : IReactiveObject
where TCollection : INotifyCollectionChanged, IEnumerable<TItem>
Expand Down
8 changes: 4 additions & 4 deletions src/ReactiveUI/Platforms/android/BundleSuspensionDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ namespace ReactiveUI;
/// <summary>
/// Loads and saves state to persistent storage.
/// </summary>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class BundleSuspensionDriver : ISuspensionDriver
{
/// <inheritdoc/>
Expand Down Expand Up @@ -71,6 +67,10 @@ public IObservable<Unit> SaveState(object state) // TODO: Create Test
}

/// <inheritdoc/>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public IObservable<Unit> InvalidateState() // TODO: Create Test
{
try
Expand Down
24 changes: 20 additions & 4 deletions src/ReactiveUI/Platforms/android/ControlFetcherMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ namespace ReactiveUI;
/// Fragments via property names, similar to Butter Knife, as well as allows
/// you to fetch controls manually.
/// </summary>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static partial class ControlFetcherMixin
{
private static readonly ConcurrentDictionary<Assembly, Dictionary<string, int>> _controlIds = new();
Expand All @@ -47,6 +43,10 @@ public static partial class ControlFetcherMixin
/// <param name="assembly">The assembly containing the user-defined view.</param>
/// <param name="propertyName">The property.</param>
/// <returns>The return view.</returns>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static View? GetControl(this View view, Assembly assembly, [CallerMemberName] string? propertyName = null) // TODO: Create Test
=> GetCachedControl(propertyName, view, () => view.FindViewById(GetControlIdByName(assembly, propertyName)));

Expand All @@ -55,6 +55,10 @@ public static partial class ControlFetcherMixin
/// </summary>
/// <param name="layoutHost">The layout view host.</param>
/// <param name="resolveMembers">The resolve members.</param>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static void WireUpControls(this ILayoutViewHost layoutHost, ResolveStrategy resolveMembers = ResolveStrategy.Implicit) // TODO: Create Test
{
ArgumentNullException.ThrowIfNull(layoutHost);
Expand All @@ -80,6 +84,10 @@ public static void WireUpControls(this ILayoutViewHost layoutHost, ResolveStrate
/// </summary>
/// <param name="view">The view.</param>
/// <param name="resolveMembers">The resolve members.</param>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static void WireUpControls(this View view, ResolveStrategy resolveMembers = ResolveStrategy.Implicit) // TODO: Create Test
{
ArgumentNullException.ThrowIfNull(view);
Expand Down Expand Up @@ -111,6 +119,10 @@ public static void WireUpControls(this View view, ResolveStrategy resolveMembers
/// <param name="fragment">The fragment.</param>
/// <param name="inflatedView">The inflated view.</param>
/// <param name="resolveMembers">The resolve members.</param>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static void WireUpControls(this Fragment fragment, View inflatedView, ResolveStrategy resolveMembers = ResolveStrategy.Implicit) // TODO: Create Test
{
ArgumentNullException.ThrowIfNull(fragment);
Expand Down Expand Up @@ -140,6 +152,10 @@ public static void WireUpControls(this Fragment fragment, View inflatedView, Res
/// </summary>
/// <param name="activity">The Activity.</param>
/// <param name="resolveMembers">The resolve members.</param>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public static void WireUpControls(this Activity activity, ResolveStrategy resolveMembers = ResolveStrategy.Implicit) // TODO: Create Test
{
ArgumentNullException.ThrowIfNull(activity);
Expand Down
4 changes: 0 additions & 4 deletions src/ReactiveUI/Platforms/android/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace ReactiveUI;
/// Android platform registrations.
/// </summary>
/// <seealso cref="ReactiveUI.IWantsToRegisterStuff" />
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class PlatformRegistrations : IWantsToRegisterStuff
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ namespace ReactiveUI;
/// <summary>
/// Loads and saves state to persistent storage.
/// </summary>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class AppSupportJsonSuspensionDriver : ISuspensionDriver
{
/// <inheritdoc/>
Expand Down Expand Up @@ -68,6 +64,10 @@ public IObservable<Unit> SaveState(object state)
}

/// <inheritdoc/>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public IObservable<Unit> InvalidateState()
{
try
Expand Down
4 changes: 0 additions & 4 deletions src/ReactiveUI/Platforms/mac/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace ReactiveUI;
/// Mac platform registrations.
/// </summary>
/// <seealso cref="ReactiveUI.IWantsToRegisterStuff" />
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class PlatformRegistrations : IWantsToRegisterStuff
{
/// <inheritdoc/>
Expand Down
4 changes: 0 additions & 4 deletions src/ReactiveUI/Platforms/net/PlatformRegistrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace ReactiveUI;
/// .NET Framework platform registrations.
/// </summary>
/// <seealso cref="ReactiveUI.IWantsToRegisterStuff" />
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class PlatformRegistrations : IWantsToRegisterStuff
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ namespace ReactiveUI;
/// UIKit platform registrations.
/// </summary>
/// <seealso cref="ReactiveUI.IWantsToRegisterStuff" />
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The method uses reflection and will not work in AOT environments.")]
[RequiresUnreferencedCode("The method uses reflection and will not work in AOT environments.")]
#endif
public class PlatformRegistrations : IWantsToRegisterStuff
{
/// <inheritdoc/>
Expand Down
Loading
Loading