Skip to content

NullabilityInfoContext throws NullReferenceException for DynamicMethod parameters #124337

@eiriktsarpalis

Description

@eiriktsarpalis

Description

NullabilityInfoContext throws a NullReferenceException when handling parameters for DynamicMethod. This was discovered when reviewing dotnet/extensions#7287, where certain nullability checks fail for dynamic methods.

Reproduction Steps

  1. Create a DynamicMethod.
  2. Attempt to retrieve nullability information for its parameters using NullabilityInfoContext.

Example code:

using System.Reflection;
using System.Reflection.Metadata;
using System.Diagnostics.CodeAnalysis;

var dynamicMethod = new DynamicMethod("TestMethod", typeof(void), new[] { typeof(int) });
var context = new NullabilityInfoContext();
var parameters = dynamicMethod.GetParameters();
foreach (var param in parameters) {
    var info = context.Create(param);
    Console.WriteLine(info.ReadState);
}

This results in a NullReferenceException.

Expected behavior

NullabilityInfoContext should either handle DynamicMethod parameters gracefully or provide a meaningful error message, not throw NullReferenceException.

Actual behavior

A NullReferenceException is thrown when attempting to analyze DynamicMethod parameters with NullabilityInfoContext, breaking expected usage.

Regression?

Unknown if this is a regression. The bug was found while reviewing recent code changes.

Known Workarounds

None known. Current workaround is to avoid passing DynamicMethod parameters to NullabilityInfoContext.

Configuration

Tested using .NET version used by dotnet/extensions and dotnet/runtime latest builds. Windows 10, x64 architecture.

Other information

Related discussion: dotnet/extensions#7287
This may need additional handling in NullabilityInfoContext's implementation for dynamic method scenarios.

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions