Description
When the method parameter type is a enum type defined in a generic class, the parameter default value can no longer be observed via Reflection API.
Reproduction Steps
The code demonstrating the problem:
using System;
public class Program
{
public static void Main()
{
Action<MyContainer<int>.MyEnum> action = MyMethod;
var type = action.Method.GetGenericMethodDefinition().GetParameters()[0].DefaultValue.GetType();
Console.WriteLine(type);
}
static void MyMethod<T>(MyContainer<T>.MyEnum param = 0)
{
}
}
class MyContainer<T>
{
public enum MyEnum
{
First,
Second
}
}
Expected behavior
Actual behavior
Unhandled exception. System.ArgumentException: Specified type is not supported
at System.RuntimeType.BoxCache..ctor(RuntimeType rt)
at System.RuntimeType.BoxCache.Create(RuntimeType type)
at System.RuntimeType.IGenericCacheEntry`1.CreateAndCache(RuntimeType type)
at System.Runtime.CompilerServices.RuntimeHelpers.Box(Byte& target, RuntimeTypeHandle type)
at System.Enum.InternalBoxEnum(RuntimeTypeHandle type, Int64 value)
at System.Reflection.MdConstant.GetValue(MetadataImport scope, Int32 token, RuntimeTypeHandle fieldTypeHandle, Boolean raw)
at System.Reflection.RuntimeParameterInfo.TryGetDefaultValueInternal(Boolean raw, Object& defaultValue)
at System.Reflection.RuntimeParameterInfo.GetDefaultValue(Boolean raw)
at Program.Main()
Regression?
This is a regression.
The last version of .NET when the code was working: 8.0
The first version of .NET when the regression appeared: 9.0
Known Workarounds
No response
Configuration
> dotnet --list-sdks
8.0.422 [C:\Program Files\dotnet\sdk]
9.0.315 [C:\Program Files\dotnet\sdk]
10.0.301 [C:\Program Files\dotnet\sdk]
Other information
No response
Description
When the method parameter type is a enum type defined in a generic class, the parameter default value can no longer be observed via Reflection API.
Reproduction Steps
The code demonstrating the problem:
Expected behavior
Actual behavior
Regression?
This is a regression.
The last version of .NET when the code was working: 8.0
The first version of .NET when the regression appeared: 9.0
Known Workarounds
No response
Configuration
Other information
No response