Reflection: Fix FormatException when querying ParameterInfo.DefaultValue of optional DateTime parameter#17877
Conversation
could it be a csproj, with the example you posted above? my understanding is that usually things are tested with ilproj, which otherwise can't be test with c# grammar? |
|
@kasper3: A I'm happy to change the tests to a |
|
@atsushikan - Could you please review this? (I am also submitting additional tests for this to CoreFX. Shall I remove the test project here completely?) |
ghost
left a comment
There was a problem hiding this comment.
As with the other one, we shouldn't include the test in coreclr, just corefx. Otherwise, LGTM
Querying the default value of an optional `DateTime` parameter using `ParameterInfo.DefaultValue` can throw a `FormatException` if that default value is a null constant in metadata, which is how the C# compiler encodes a default value of `default(DateTime)`. This commit fixes that error by adding the proper handling for null metadata constants with `DateTime` parameters.
|
@atsushikan - removed all test code & rebased. |
Summary:
Fixes a reflection bug (as discussed in https://github.com/dotnet/corefx/issues/26164) which causes a
FormatExceptionwhen callingParameterInfo.DefaultValuefor an optionalDateTimeparameter having an encoded default value ofnull(which is how the Roslyn C# compiler encodesdefault(DateTime)).Example of the bug being fixed:
The following C# program currently throws an exception:
This PR makes the above program succeed.
Some additional notes:
Reflection reporting a default value
nullwhen it'sdefault(DateTime)in source code may seem surprising, but is in line with reflection's behavior for how C# encodes otherdefault(<any value type>).I'm including a test projecttests/src/reflection/OptionalParameters/DefaultValue.csproj.tests/src/reflection/OptionalParameters/DefaultValue.ilproj, however it doesn't currently get built automatically (see Refine test discovery and partition during test build with new target #17331). I've run it manually to verify this bugfix.