Enhances Nullable Reference Type support#3399
Conversation
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.
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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 👍 / 👎.
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.
Description of Change
This pull request significantly enhances Nullable Reference Type (NRT) support across
Prism.Corecomponents. It updates method signatures and internal collections, such asIParameters,ParametersBase,NavigationParameters, and various extension methods, to explicitly allow or handle nullableobjectvalues. This involves annotatingobjecttypes with?where null is permissible and employing null-forgiving operators (!) where non-nullability is asserted by context. TheTryGetValuepattern also benefits from the[MaybeNullWhen(false)]attribute, providing clearer nullability contracts foroutparameters.Additionally, several .NET Framework-specific serialization attributes and constructors for exceptions within the
Prism.Core.ModularityandPrism.Core.Navigation.Regionsnamespaces 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 inPropertySupportand explicit type casting inViewRegistryBaseto prevent NRT warnings.Bugs Fixed
API Changes
Changed:
Addmethods inIParameters,ParametersBase, andINavigationParametersInternalnow acceptobject?values instead ofobject.ParametersExtensionsmethods (e.g.,GetValue,TryGetValue,GetValues,ContainsKey) that previously acceptedIEnumerable>now acceptIEnumerable>.ParametersExtensions.GetValue(this IEnumerable> parameters, string key, Type type)changed fromobjecttoobject?.outparameter inParametersExtensions.TryGetValuenow 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