-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant
Fixes #50439
- Loading branch information
1 parent
a85d36f
commit 54239d2
Showing
5 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Microsoft.Extensions.Internal | ||
{ | ||
internal static class ParameterDefaultValue | ||
{ | ||
public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) | ||
{ | ||
bool tryToGetDefaultValue = true; | ||
defaultValue = null; | ||
bool hasDefaultValue = parameter.HasDefaultValue; | ||
|
||
if (hasDefaultValue) | ||
{ | ||
if (tryToGetDefaultValue) | ||
{ | ||
defaultValue = parameter.DefaultValue; | ||
} | ||
|
||
bool isNullableParameterType = parameter.ParameterType.IsGenericType && | ||
parameter.ParameterType.GetGenericTypeDefinition() == typeof(Nullable<>); | ||
|
||
// Workaround for https://github.com/dotnet/runtime/issues/18599 | ||
if (defaultValue == null && parameter.ParameterType.IsValueType | ||
&& !isNullableParameterType) // Nullable types should be left null | ||
{ | ||
defaultValue = CreateValueType(parameter.ParameterType); | ||
} | ||
|
||
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", | ||
Justification = "CreateValueType is only called on a ValueType. You can always create an instance of a ValueType.")] | ||
static object? CreateValueType(Type t) => | ||
RuntimeHelpers.GetUninitializedObject(t); | ||
|
||
// Handle nullable enums | ||
if (defaultValue != null && isNullableParameterType) | ||
{ | ||
Type? underlyingType = Nullable.GetUnderlyingType(parameter.ParameterType); | ||
if (underlyingType != null && underlyingType.IsEnum) | ||
{ | ||
defaultValue = Enum.ToObject(underlyingType, defaultValue); | ||
} | ||
} | ||
} | ||
|
||
return hasDefaultValue; | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters