Skip to content

Under what circumstances can System.Reflection.ParameterInfo.Name be null? #73188

@eiriktsarpalis

Description

@eiriktsarpalis

Consider the following console app:

using System;
using System.Reflection;

Type type = Assembly.GetExecutingAssembly().GetType("System.Runtime.CompilerServices.NullableContextAttribute")!;
ParameterInfo parameterInfo = type.GetConstructor(new Type[] { typeof(byte) })!.GetParameters()[0];
Console.WriteLine(parameterInfo.Name is not null); // Expected "P_0", got null

// Force generation of a NullableContextAttribute in this assembly
static string? MethodWithNullableAnnotations() => throw new NotImplementedException();

// Generated attribute:
//
//[CompilerGenerated]
//[Microsoft.CodeAnalysis.Embedded]
//[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
//internal sealed class NullableContextAttribute : Attribute
//{
//    public readonly byte Flag;

//    public NullableContextAttribute(byte P_0)
//    {
//        Flag = P_0;
//    }
//}

I'm not sure if this is a bug or not, but I would have expected parameterInfo.Name to be non-null in this case. The issue does not reproduce if I manually define the attribute as decompiled. Documentation for ParameterInfo.Name seems to suggest that Name can be null if the parameter is obtained via MethodInfo.ReturnParameter but this is not the case in this reproduction. Can reproduce the same behavior in both .NET 7 and netfx runtimes.

Discovered while investigating #58690.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ReflectionquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions