-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
FormatException
when callingParameterInfo.DefaultValue
for an optionalDateTime
parameter 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
null
when 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.