Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Reflection: Fix DefaultValue exception with enums (#17917)
Browse files Browse the repository at this point in the history
This is related to dbcfd2f, but about enums instead of `DateTime`.

Given e.g. a generic method with a parameter `T arg = default(T)`
where `T` is the generic type parameter, if that is instantiated with
some enum as the generic type argument, then querying the default
value of `arg` using `ParameterInfo.[Raw]DefaultValue` will throw a
`FormatException`.

(The use of generics means that the C# compiler always records a
`null` constant in metadata, which isn't usually the case for enums.)
  • Loading branch information
stakx authored and atsushikan committed May 8, 2018
1 parent 2d68d03 commit 6f0bb94
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/mscorlib/src/System/Reflection/MdConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTyp
defaultValue = buffer;
break;

case CorElementType.Class:
return null;

default:
throw new FormatException(SR.Arg_BadLiteralFormat);
#endregion
Expand Down

0 comments on commit 6f0bb94

Please sign in to comment.