From 54239d221cf2ac0240a7bdb7139756d623ccbde5 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 26 Jul 2021 13:49:09 -0700 Subject: [PATCH 1/8] Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant Fixes #50439 --- .../ParameterDefaultValue.netcoreapp.cs | 57 +++++++++++++++++++ ...s => ParameterDefaultValue.netstandard.cs} | 0 .../src/ActivatorUtilities.cs | 4 +- ...ns.DependencyInjection.Abstractions.csproj | 17 ++++-- ...soft.Extensions.DependencyInjection.csproj | 36 +++++++++--- 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs rename src/libraries/Common/src/Extensions/ParameterDefaultValue/{ParameterDefaultValue.cs => ParameterDefaultValue.netstandard.cs} (100%) diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs new file mode 100644 index 0000000000000..cfc50955763bd --- /dev/null +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs @@ -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; + } + } +} diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs similarity index 100% rename from src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs rename to src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs index 94bbfdb2a4533..99ef9aec6b3ae 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs @@ -98,10 +98,10 @@ public static ObjectFactory CreateFactory( ParameterExpression? argumentArray = Expression.Parameter(typeof(object[]), "argumentArray"); Expression? factoryExpressionBody = BuildFactoryExpression(constructor, parameterMap, provider, argumentArray); - var factoryLambda = Expression.Lambda>( + var factoryLambda = Expression.Lambda>( factoryExpressionBody, provider, argumentArray); - Func? result = factoryLambda.Compile(); + Func? result = factoryLambda.Compile(); return result.Invoke; } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj index c6eff6edf2d49..8a7bc21737b3c 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj @@ -1,7 +1,7 @@ - netstandard2.1;netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461 true enable Abstractions for dependency injection. @@ -10,9 +10,18 @@ Commonly Used Types: Microsoft.Extensions.DependencyInjection.IServiceCollection - - + + + + + + + + + + diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 9884fdceeaecb..2825492fea6f5 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -1,7 +1,7 @@ - netstandard2.1;netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461 False Annotations @@ -17,21 +17,43 @@ $(DefineConstants);SAVE_ASSEMBLIES + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - From 3b46a545499ce8ce6a0a592aa816d98c01e6d50c Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 26 Jul 2021 14:09:37 -0700 Subject: [PATCH 2/8] code cleanup --- ...ns.DependencyInjection.Abstractions.csproj | 11 ++++--- ...soft.Extensions.DependencyInjection.csproj | 29 ++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj index 8a7bc21737b3c..ef6baca6d070b 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj @@ -11,10 +11,6 @@ Microsoft.Extensions.DependencyInjection.IServiceCollection - - - - @@ -33,4 +29,11 @@ Microsoft.Extensions.DependencyInjection.IServiceCollection + + + + + + + diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 2825492fea6f5..299c6f3659008 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -18,19 +18,6 @@ - - - - - - - - - - - - - @@ -67,4 +54,20 @@ + + + + + + + + + + + + + + + + From 6b033acde29b6a261aeafb8a6b0514079983d0bb Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 26 Jul 2021 14:13:47 -0700 Subject: [PATCH 3/8] enable nullable for netcoreapp --- .../src/Microsoft.Extensions.DependencyInjection.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 299c6f3659008..847b896791716 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -5,6 +5,7 @@ False Annotations + enable $(NoWarn);CP0001 Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection. From 00acb0cc7e0c16a95cacc4abe770dd973a617403 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 26 Jul 2021 14:19:20 -0700 Subject: [PATCH 4/8] move to second property group --- .../src/Microsoft.Extensions.DependencyInjection.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 847b896791716..0eba1de025e5c 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -5,7 +5,6 @@ False Annotations - enable $(NoWarn);CP0001 Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection. @@ -14,6 +13,7 @@ True + enable $(DefineConstants);IL_EMIT $(DefineConstants);SAVE_ASSEMBLIES From ebbf4e54305bfd4319136caa18bf3eb31090374c Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Mon, 26 Jul 2021 17:00:49 -0700 Subject: [PATCH 5/8] Apply PR feedback --- .../ParameterDefaultValue.cs | 51 ++++++++++++++ .../ParameterDefaultValue.netcoreapp.cs | 48 +++---------- .../ParameterDefaultValue.netstandard.cs | 68 ++++--------------- ...ns.DependencyInjection.Abstractions.csproj | 14 ++-- ...soft.Extensions.DependencyInjection.csproj | 23 +------ 5 files changed, 82 insertions(+), 122 deletions(-) create mode 100644 src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs new file mode 100644 index 0000000000000..d229afb924800 --- /dev/null +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs @@ -0,0 +1,51 @@ +// 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; + +namespace Microsoft.Extensions.Internal +{ + internal static partial class ParameterDefaultValue + { + public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) + { + bool hasDefaultValue = CheckHasDefaultValue(parameter); + bool tryToGetDefaultValue = true; + defaultValue = null; + + 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); + } + + // 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; + } + } +} diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs index cfc50955763bd..0c02474d34c7a 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs @@ -10,48 +10,16 @@ namespace Microsoft.Extensions.Internal { - internal static class ParameterDefaultValue + internal static partial class ParameterDefaultValue { - public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) + public static bool CheckHasDefaultValue(ParameterInfo parameter) { - 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; + return parameter.HasDefaultValue; } + + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", + Justification = "CreateValueType is only called on a ValueType. You can always create an instance of a ValueType.")] + internal static object? CreateValueType(Type t) => + RuntimeHelpers.GetUninitializedObject(t); } } diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs index f69b52220f355..e31f5a4994eb8 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs @@ -1,6 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#nullable enable// 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; @@ -15,65 +18,20 @@ namespace Microsoft.Extensions.Internal { - internal static class ParameterDefaultValue + internal static partial class ParameterDefaultValue { - public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) + public static bool CheckHasDefaultValue(ParameterInfo parameter) { - bool hasDefaultValue; - bool tryToGetDefaultValue = true; - defaultValue = null; - - try - { - hasDefaultValue = parameter.HasDefaultValue; - } - catch (FormatException) when (parameter.ParameterType == typeof(DateTime)) - { - // Workaround for https://github.com/dotnet/runtime/issues/18844 - // If HasDefaultValue throws FormatException for DateTime - // we expect it to have default value - hasDefaultValue = true; - tryToGetDefaultValue = false; - } - - 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); - } + return parameter.HasDefaultValue; + } - [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) => + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", + Justification = "CreateValueType is only called on a ValueType. You can always create an instance of a ValueType.")] + internal static object? CreateValueType(Type t) => #if NETFRAMEWORK || NETSTANDARD2_0 - FormatterServices.GetUninitializedObject(t); + FormatterServices.GetUninitializedObject(t); #else - RuntimeHelpers.GetUninitializedObject(t); + RuntimeHelpers.GetUninitializedObject(t); #endif - - // 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; - } } -} +} \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj index ef6baca6d070b..83b4966493786 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj @@ -8,8 +8,15 @@ Commonly Used Types: Microsoft.Extensions.DependencyInjection.IServiceCollection + + false + + + + @@ -29,11 +36,4 @@ Microsoft.Extensions.DependencyInjection.IServiceCollection - - - - - - - diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 0eba1de025e5c..74f3a226bfcbf 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -13,9 +13,9 @@ True - enable $(DefineConstants);IL_EMIT $(DefineConstants);SAVE_ASSEMBLIES + false @@ -26,7 +26,6 @@ - @@ -36,9 +35,9 @@ - - + @@ -55,20 +54,4 @@ - - - - - - - - - - - - - - - - From a4a9fb1f8b794c3bceab2ea49a3db44adf8121be Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Wed, 28 Jul 2021 17:25:46 -0700 Subject: [PATCH 6/8] Apply PR feedback --- .../ParameterDefaultValue.cs | 18 ++++++++++-- .../ParameterDefaultValue.netcoreapp.cs | 8 ++---- .../ParameterDefaultValue.netstandard.cs | 28 +++++++++---------- ...ns.DependencyInjection.Abstractions.csproj | 4 +-- ...soft.Extensions.DependencyInjection.csproj | 6 ++-- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs index d229afb924800..535d8ff3679a7 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs @@ -7,14 +7,19 @@ using System.Diagnostics.CodeAnalysis; using System.Reflection; +#if NETFRAMEWORK || NETSTANDARD2_0 +using System.Runtime.Serialization; +#else +using System.Runtime.CompilerServices; +#endif + namespace Microsoft.Extensions.Internal { internal static partial class ParameterDefaultValue { public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) { - bool hasDefaultValue = CheckHasDefaultValue(parameter); - bool tryToGetDefaultValue = true; + bool hasDefaultValue = CheckHasDefaultValue(parameter, out bool tryToGetDefaultValue); defaultValue = null; if (hasDefaultValue) @@ -34,6 +39,15 @@ public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defau 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) => +#if NETFRAMEWORK || NETSTANDARD2_0 + FormatterServices.GetUninitializedObject(t); +#else + RuntimeHelpers.GetUninitializedObject(t); +#endif + // Handle nullable enums if (defaultValue != null && isNullableParameterType) { diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs index 0c02474d34c7a..484fc384df1f3 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs @@ -12,14 +12,10 @@ namespace Microsoft.Extensions.Internal { internal static partial class ParameterDefaultValue { - public static bool CheckHasDefaultValue(ParameterInfo parameter) + public static bool CheckHasDefaultValue(ParameterInfo parameter, out bool tryToGetDefaultValue) { + tryToGetDefaultValue = true; return parameter.HasDefaultValue; } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", - Justification = "CreateValueType is only called on a ValueType. You can always create an instance of a ValueType.")] - internal static object? CreateValueType(Type t) => - RuntimeHelpers.GetUninitializedObject(t); } } diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs index e31f5a4994eb8..6dc03f9f75588 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs @@ -1,9 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable// 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; @@ -20,18 +17,21 @@ namespace Microsoft.Extensions.Internal { internal static partial class ParameterDefaultValue { - public static bool CheckHasDefaultValue(ParameterInfo parameter) + public static bool CheckHasDefaultValue(ParameterInfo parameter, out bool tryToGetDefaultValue) { - return parameter.HasDefaultValue; + tryToGetDefaultValue = true; + try + { + return parameter.HasDefaultValue; + } + catch (FormatException) when (parameter.ParameterType == typeof(DateTime)) + { + // Workaround for https://github.com/dotnet/runtime/issues/18844 + // If HasDefaultValue throws FormatException for DateTime + // we expect it to have default value + tryToGetDefaultValue = false; + return true; + } } - - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067:UnrecognizedReflectionPattern", - Justification = "CreateValueType is only called on a ValueType. You can always create an instance of a ValueType.")] - internal static object? CreateValueType(Type t) => -#if NETFRAMEWORK || NETSTANDARD2_0 - FormatterServices.GetUninitializedObject(t); -#else - RuntimeHelpers.GetUninitializedObject(t); -#endif } } \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj index 83b4966493786..b275c71c780c7 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/Microsoft.Extensions.DependencyInjection.Abstractions.csproj @@ -17,12 +17,12 @@ Microsoft.Extensions.DependencyInjection.IServiceCollection Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" /> - + - + diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 74f3a226bfcbf..4111ca6b5bc67 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -8,6 +8,7 @@ $(NoWarn);CP0001 Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection. + false @@ -15,15 +16,14 @@ True $(DefineConstants);IL_EMIT $(DefineConstants);SAVE_ASSEMBLIES - false - + - + From 920a08626ec2ab7fd42b42d4d0a5869db3adcab4 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Thu, 29 Jul 2021 14:57:58 -0700 Subject: [PATCH 7/8] code cleanup - remove ifdef --- .../ParameterDefaultValue.netstandard.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs index 6dc03f9f75588..21e9fdf849a9f 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs @@ -6,12 +6,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; - -#if NETFRAMEWORK || NETSTANDARD2_0 using System.Runtime.Serialization; -#else -using System.Runtime.CompilerServices; -#endif namespace Microsoft.Extensions.Internal { From f2e0a290761ff4b40461c779d1658f1599249295 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 30 Jul 2021 10:15:38 -0700 Subject: [PATCH 8/8] Update src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj Co-authored-by: Viktor Hofer --- .../src/Microsoft.Extensions.DependencyInjection.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj index 4111ca6b5bc67..b93c0a8e61e66 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -8,6 +8,7 @@ $(NoWarn);CP0001 Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection. + false