From ddee7f207886ed0b36aba13f63c6a413eef0b78f Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 18 Feb 2026 11:40:10 +0100 Subject: [PATCH 1/2] [ObjCRuntime] Enable nullability and clean up BackingField. This is file 4 of 7 files with nullability disabled in ObjCRuntime. * Enable nullability (#nullable enable). * Add nullable annotations for cached backing-field helper APIs. * Add converter null checks using ThrowHelper to improve static analysis. * Add XML documentation for BackingField and its public helper methods. Contributes towards https://github.com/dotnet/macios/issues/17285. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/ObjCRuntime/BackingField.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ObjCRuntime/BackingField.cs b/src/ObjCRuntime/BackingField.cs index 3692df3c3dda..c1ae578d3d61 100644 --- a/src/ObjCRuntime/BackingField.cs +++ b/src/ObjCRuntime/BackingField.cs @@ -37,15 +37,23 @@ #if IOS -// Disable until we get around to enable + fix any issues. -#nullable disable +#nullable enable namespace ObjCRuntime { + /// Helpers to convert between native handles and cached managed wrappers. static class BackingField { - public static T Get (ref T value, IntPtr handle, Converter c) + /// Gets a cached managed wrapper or creates a new one from a native handle. + /// The managed wrapper type. + /// The cached managed value. + /// The native handle. + /// A converter that creates a managed wrapper from a native handle. + /// The cached or newly created managed value, or if is . + public static T? Get (ref T? value, IntPtr handle, Converter c) where T : class, INativeObject, IDisposable { + c = ThrowHelper.ThrowIfNull (c); + if (handle == IntPtr.Zero) { if (value is not null) value.Dispose (); @@ -59,7 +67,12 @@ public static T Get (ref T value, IntPtr handle, Converter c) return value = c (handle); } - public static IntPtr Save (ref T value, T newValue) + /// Saves a managed wrapper in a backing field and returns the corresponding native handle. + /// The managed wrapper type. + /// The current cached managed value. + /// The new managed value to cache. + /// The native handle for , or if is . + public static IntPtr Save (ref T? value, T? newValue) where T : class, INativeObject, IDisposable { #pragma warning disable RBI0014 From 3c02f6c2b3536782e09ff2226bc8878bb6998e79 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 19 Feb 2026 10:33:13 +0100 Subject: [PATCH 2/2] Fix build --- src/ObjCRuntime/BackingField.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ObjCRuntime/BackingField.cs b/src/ObjCRuntime/BackingField.cs index c1ae578d3d61..333f4b6efbcb 100644 --- a/src/ObjCRuntime/BackingField.cs +++ b/src/ObjCRuntime/BackingField.cs @@ -49,7 +49,7 @@ static class BackingField { /// The native handle. /// A converter that creates a managed wrapper from a native handle. /// The cached or newly created managed value, or if is . - public static T? Get (ref T? value, IntPtr handle, Converter c) + public static T? Get (ref T? value, IntPtr handle, Converter c) where T : class, INativeObject, IDisposable { c = ThrowHelper.ThrowIfNull (c);