Skip to content

Expose nullable reference type metadata across all target frameworks - #174

Merged
dennisdoomen merged 2 commits into
mainfrom
dennisdoomen-expose-nullable-reference-metadata
Aug 10, 2026
Merged

Expose nullable reference type metadata across all target frameworks#174
dennisdoomen merged 2 commits into
mainfrom
dennisdoomen-expose-nullable-reference-metadata

Conversation

@dennisdoomen

Copy link
Copy Markdown
Owner

Closes #162

Summary

Adds a Nullability enum (Unknown, NotNull, Nullable) plus GetNullability()/IsNullableReference() extension methods for PropertyInfo, FieldInfo and ParameterInfo, working consistently across all four target frameworks (net47, net6.0, netstandard2.0, netstandard2.1).

Implementation

  • Value types (including Nullable<T>) short-circuit using the existing NullableOrActualType helper: intNotNull, int?Nullable.
  • On net6.0 and later, reference type nullability is determined via System.Reflection.NullabilityInfoContext for correctness and simplicity.
  • On net47, netstandard2.0 and netstandard2.1, a new internal NullabilityMetadataReader reads the compiler-emitted System.Runtime.CompilerServices.NullableAttribute (falling back to NullableContextAttribute on the declaring type, then its ancestors, then the module) to determine the same information. Since these attributes are compiler-internal and emitted per-assembly, they're matched by full type name rather than typeof(...).
  • IsNullableReference() is a convenience shortcut for GetNullability() == Nullability.Nullable.

Tests

Added spec classes covering non-nullable/nullable reference properties, fields and parameters, value types, nullable value types, generic reference types (List<string> vs List<string>?), and members compiled without a nullable context (Unknown).

Validation

  • dotnet build src\Reflectify\Reflectify.csproj — succeeds for all 4 TFMs.
  • dotnet test tests\Reflectify.Specs\Reflectify.Specs.csproj — all tests pass on net8.0, net6.0 and net472 (the netcoreapp3.0 leg fails locally only because that runtime isn't installed in this environment, which is a pre-existing/unrelated environment limitation).

Docs

Updated the "How do I use it?" section of README.md to document the new methods.

…meterInfo

Closes #162

Add a public/internal Nullability enum and GetNullability/IsNullableReference
extension methods for PropertyInfo, FieldInfo and ParameterInfo. On net6.0+
this delegates to System.Reflection.NullabilityInfoContext; on net47,
netstandard2.0 and netstandard2.1 it reads the compiler-emitted
NullableAttribute/NullableContextAttribute metadata directly via a shared
NullabilityMetadataReader. Value types (including Nullable<T>) are
short-circuited using the existing NullableOrActualType helper.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Test Results

  4 files  ±  0    4 suites  ±0   3m 57s ⏱️ -18s
214 tests + 27  214 ✅ + 27  0 💤 ±0  0 ❌ ±0 
851 runs  +108  851 ✅ +108  0 💤 ±0  0 ❌ ±0 

Results for commit 4875709. ± Comparison against base commit 1a9ca26.

@dennisdoomen dennisdoomen added the enhancement New feature or request label Aug 10, 2026
The fallback attribute-reading logic is only used on frameworks without
System.Reflection.NullabilityInfoContext. It was previously compiled
unconditionally into every TFM, which meant it showed up as entirely
uncovered/unreachable code on net6.0+ builds where the fast path is used
instead, dragging down overall coverage. Wrapping the whole type in
#if !NET6_0_OR_GREATER excludes it from those builds so coverage tooling
no longer counts it there.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dennisdoomen
dennisdoomen enabled auto-merge (squash) August 10, 2026 15:41
@dennisdoomen
dennisdoomen merged commit 5bf3449 into main Aug 10, 2026
2 checks passed
@dennisdoomen
dennisdoomen deleted the dennisdoomen-expose-nullable-reference-metadata branch August 10, 2026 15:46
dennisdoomen pushed a commit that referenced this pull request Aug 10, 2026
Resolve conflicts with PR #174 (nullable reference type metadata) by
keeping both sets of additions in PropertyInfoExtensions.cs,
FieldInfoExtensions.cs, their specs, and README.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Expose nullable reference type metadata across all target frameworks

1 participant