From: stakx Date: Mon, 14 May 2018 14:20:53 +0000 (+0200) Subject: Reflection: Allow ParameterBuilder.SetConstant(nonNullValue) for nullable enum parame... X-Git-Tag: submit/tizen/20210909.063632~11030^2~4849 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d9736d2c4dcbc8bbcde026bdce20812f67596d5b;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Reflection: Allow ParameterBuilder.SetConstant(nonNullValue) for nullable enum parameters (dotnet/coreclr#17977) * ParameterBuilder.SetConstant(nonNull) for TEnum? This change makes it possible to use `ParameterBuilder.SetConstant` to set a non-null default value for parameters having a nullable enum type. * ParameterInfo.DefaultValue for TEnum? Add a note to `MdConstant.GetValue` (which sits behind `ParameterInfo. [Raw]DefaultValue`) explaining why changing its behavior to mirror the change in `TypeBuilder.SetConstantValue` would be a breaking change. Commit migrated from https://github.com/dotnet/coreclr/commit/f1c47fa7d2cdf29c5d0d1d7c89855e0d0ee5518a --- diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs index f517decc1d8..7fc7132f406 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs @@ -301,6 +301,10 @@ namespace System.Reflection.Emit if (destType.IsByRef) destType = destType.GetElementType(); + // Convert nullable types to their underlying type. + // This is necessary for nullable enum types to pass the IsEnum check that's coming next. + destType = Nullable.GetUnderlyingType(destType) ?? destType; + if (destType.IsEnum) { // | UnderlyingSystemType | Enum.GetUnderlyingType() | IsEnum diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/MdConstant.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/MdConstant.cs index 5284bcae0b1..e7e0684785f 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/MdConstant.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/MdConstant.cs @@ -23,6 +23,12 @@ namespace System.Reflection if (fieldType.IsEnum && raw == false) { + // NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes + // a nullable enum type `Nullable`, we do not unpack it to `TEnum` to + // successfully enter this `if` clause. Default values of `TEnum?`-typed + // parameters have been reported as values of the underlying type, changing + // this now might be a breaking change. + long defaultValue = 0; switch (corElementType)