-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Under what circumstances can System.Reflection.ParameterInfo.Name
be null?
#73188
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsConsider 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 Discovered while investigating #58690.
|
This prints "True": public class Program
{
private static void Main() =>
Console.WriteLine(typeof(Program).GetMethod("M")!.ReturnParameter.Name is null);
public static object M() => new object();
} (The title of this issue is about PropertyInfo, but the contents is about ParameterInfo... I assume you meant the latter) |
Oh, I misunderstood your question. You're not asking whether there are any situations where it can be null and arguing it can't, you're asking why it's null in the specific situation in your repro? |
System.Reflection.PropertyInfo.Name
be null?System.Reflection.ParameterInfo.Name
be null?
Correct. |
Parameter names are optional in IL. In the case in your repro, I expect Roslyn doesn't emit one for the NullableContextAttribute's ctor parameter it synthesizes. Our own linker will even strip parameter names in some builds in order to reduce size. |
This expected "P_0" because that's what ILSpy renders? I believe this is just ILSpy trying to create valid C# and manufacturing a parameter name when one doesn't exist, e.g. |
Gotcha. It would seem that
|
Consider the following console app:
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 forParameterInfo.Name
seems to suggest thatName
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.
The text was updated successfully, but these errors were encountered: