Closed
Description
Description
NullabilityInfoContext
will throw IndexOutOfRangeException
when calling Create
method on certain Dictionary
-valued properties in generic classes. Seems to happen when the TValue
of the dictionary is a generic class with one type parameter coinciding with the type parameter of the class that defines the property.
Reproduction Steps
// .NET 6.0 LTS Console App project with default settings (nullability context enabled).
using System.Reflection;
Foo<int>? bar = new();
NullabilityInfoContext context = new();
PropertyInfo x = bar.GetType().GetProperty(nameof(bar.Bad))!;
// The next line will throw.
NullabilityInfo info = context.Create(x);
Console.WriteLine(info.ReadState);
public class Foo<T>
{
public Dictionary<int, List<T>>? Bad { get; set; }
public Dictionary<int, IEquatable<T>>? AlsoBad { get; set; }
public Dictionary<int, List<int>>? Good { get; set; }
public Dictionary<int, T>? AlsoGood { get; set; }
}
Expected behavior
Program should successfully terminate with Nullable displayed on the console.
Actual behavior
Line NullabilityInfo info = context.Create(x);
throws a System.IndexOutOfRangeException
.
at System.Reflection.NullabilityInfoContext.CheckGenericParameters(NullabilityInfo nullability, MemberInfo metaMember, Type metaType)
at System.Reflection.NullabilityInfoContext.TryLoadGenericMetaTypeNullability(MemberInfo memberInfo, NullabilityInfo nullability)
at System.Reflection.NullabilityInfoContext.GetNullabilityInfo(MemberInfo memberInfo, Type type, IList`1 customAttributes, Int32 index)
at System.Reflection.NullabilityInfoContext.GetNullabilityInfo(MemberInfo memberInfo, Type type, IList`1 customAttributes, Int32 index)
at System.Reflection.NullabilityInfoContext.Create(PropertyInfo propertyInfo)
Regression?
No response
Known Workarounds
No response
Configuration
This is a vanilla .NET 6.0 Long-term Support "Console App" project created in Visual Studio 2022 on a Windows 11 machine. Fails in both Debug and Release mode when targeting "Any CPU".
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Other information
No response