Skip to content

Enhances Nullable Reference Type support#3399

Closed
dansiegel wants to merge 3 commits into
masterfrom
dev/ds/core-warnings
Closed

Enhances Nullable Reference Type support#3399
dansiegel wants to merge 3 commits into
masterfrom
dev/ds/core-warnings

Conversation

@dansiegel
Copy link
Copy Markdown
Member

Description of Change

This pull request significantly enhances Nullable Reference Type (NRT) support across Prism.Core components. It updates method signatures and internal collections, such as IParameters, ParametersBase, NavigationParameters, and various extension methods, to explicitly allow or handle nullable object values. This involves annotating object types with ? where null is permissible and employing null-forgiving operators (!) where non-nullability is asserted by context. The TryGetValue pattern also benefits from the [MaybeNullWhen(false)] attribute, providing clearer nullability contracts for out parameters.

Additionally, several .NET Framework-specific serialization attributes and constructors for exceptions within the Prism.Core.Modularity and Prism.Core.Navigation.Regions namespaces are now conditionally compiled to ensure they are only included when targeting .NET Framework. This reduces compiler warnings and improves compatibility with modern .NET versions. Other minor updates include safer null checks for property getters in PropertySupport and explicit type casting in ViewRegistryBase to prevent NRT warnings.

Bugs Fixed

  • None explicitly. Addresses numerous compiler warnings related to Nullable Reference Types.

API Changes

Changed:

  • The Add methods in IParameters, ParametersBase, and INavigationParametersInternal now accept object? values instead of object.
  • Many parameter types in ParametersExtensions methods (e.g., GetValue, TryGetValue, GetValues, ContainsKey) that previously accepted IEnumerable> now accept IEnumerable>.
  • The return type of ParametersExtensions.GetValue(this IEnumerable> parameters, string key, Type type) changed from object to object?.
  • The out parameter in ParametersExtensions.TryGetValue now includes the [MaybeNullWhen(false)] attribute for improved nullability analysis.

Behavioral Changes

No significant behavioral changes are introduced for end-users. The modifications are primarily focused on compile-time nullability checks and addressing compiler warnings, ensuring the codebase is more robust and explicit about its nullability contracts without altering core runtime logic.

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of master at time of PR
  • Changes adhere to coding standard

This change primarily focuses on:
- Updating `IParameters` and related implementations to support `object?` values, which improves nullability analysis and reduces potential `NullReferenceException`s.
- Introducing null-forgiving operators (`!`) and explicit null checks where appropriate to comply with Nullable Reference Type (NRT) strictness.
- Adding conditional compilation for `[Serializable]` attributes and serialization constructors on exception types (e.g., Modularity, Region exceptions) for .NET Framework targets. This resolves build warnings and ensures better compatibility across different .NET target frameworks (e.g., .NET 6+).
- Addressing minor nullability-related issues in `PropertySupport` and `ViewRegistryBase`.

These updates modernize the `Prism.Core` codebase, leverage C# 8+ NRT features, and ensure cleaner, warning-free compilation across multi-targeted projects.
@dansiegel dansiegel requested a review from brianlagunas as a code owner May 13, 2026 14:14
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 218017fa76

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/// <returns>Success if value is found; otherwise returns <c>false</c></returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)
public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object?>> parameters, string key, [MaybeNullWhen(false)] out T value)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Return true for stored null parameter values

Because Add now explicitly accepts nullable values, callers can store an explicit null and should still be able to distinguish that from a missing key. This TryGetValue<T> path still only returns from the loop when valueAsObject is T, which is false for null, so TryGetValue<object?>("key", out _) or TryGetValue<string?>(...) reports false even though the key exists, contradicting the interface contract that the bool indicates whether the collection contains the key.

Useful? React with 👍 / 👎.

dansiegel added 2 commits May 13, 2026 08:31
Changes `GlobalPackageReference` to `PackageReference` in `Directory.Build.props` for several packages. This ensures consistent package referencing when Central Package Management (CPM) is enabled, as `GlobalPackageReference` is primarily used for version definitions within `Directory.Packages.props`.
This change updates the `ErrorsContainer` class to leverage modern C# language features and .NET APIs:

- Uses target-typed `new` expressions for concise collection and array initialization (C# 9+).
- Converts the `HasErrors` property to an expression-bodied member (C# 6+).
- Introduces conditional compilation to utilize `ArgumentNullException.ThrowIfNull` for modern .NET targets, while falling back to manual null checks for .NET Framework compatibility.
- Removes redundant `this.` qualifiers for improved readability and consistency.

These updates align with ongoing efforts to modernize the codebase and improve adherence to contemporary C# standards.
@dansiegel dansiegel closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant