From 6f0bb947138c6f75a1721fef7f6c54d4b01282dc Mon Sep 17 00:00:00 2001 From: stakx Date: Tue, 8 May 2018 19:42:25 +0200 Subject: [PATCH] Reflection: Fix DefaultValue exception with enums (#17917) This is related to dbcfd2f9d1, 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.) --- src/mscorlib/src/System/Reflection/MdConstant.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mscorlib/src/System/Reflection/MdConstant.cs b/src/mscorlib/src/System/Reflection/MdConstant.cs index 733703990814..5284bcae0b1d 100644 --- a/src/mscorlib/src/System/Reflection/MdConstant.cs +++ b/src/mscorlib/src/System/Reflection/MdConstant.cs @@ -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