From 222c61355dfb28c0025ca00e70dea7ec2f347b5d Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Fri, 30 Jul 2021 12:53:01 -0700 Subject: [PATCH] Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant (#56324) * Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant Fixes #50439 * code cleanup * enable nullable for netcoreapp * move to second property group * Apply PR feedback * Apply PR feedback * code cleanup - remove ifdef * Update src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj Co-authored-by: Viktor Hofer Co-authored-by: Viktor Hofer --- .../ParameterDefaultValue/ParameterDefaultValue.cs | 18 ++---------- .../ParameterDefaultValue.netcoreapp.cs | 21 ++++++++++++++ .../ParameterDefaultValue.netstandard.cs | 32 ++++++++++++++++++++++ .../src/ActivatorUtilities.cs | 4 +-- ...ensions.DependencyInjection.Abstractions.csproj | 14 +++++++++- ...Microsoft.Extensions.DependencyInjection.csproj | 24 +++++++++++----- 6 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs create mode 100644 src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs index f69b522..535d8ff 100644 --- a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.cs @@ -15,27 +15,13 @@ using System.Runtime.CompilerServices; namespace Microsoft.Extensions.Internal { - internal static class ParameterDefaultValue + internal static partial class ParameterDefaultValue { public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) { - bool hasDefaultValue; - bool tryToGetDefaultValue = true; + bool hasDefaultValue = CheckHasDefaultValue(parameter, out bool tryToGetDefaultValue); 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) 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 0000000..484fc38 --- /dev/null +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netcoreapp.cs @@ -0,0 +1,21 @@ +// 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 partial class ParameterDefaultValue + { + public static bool CheckHasDefaultValue(ParameterInfo parameter, out bool tryToGetDefaultValue) + { + tryToGetDefaultValue = true; + return parameter.HasDefaultValue; + } + } +} diff --git a/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs new file mode 100644 index 0000000..21e9fdf --- /dev/null +++ b/src/libraries/Common/src/Extensions/ParameterDefaultValue/ParameterDefaultValue.netstandard.cs @@ -0,0 +1,32 @@ +// 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.Serialization; + +namespace Microsoft.Extensions.Internal +{ + internal static partial class ParameterDefaultValue + { + public static bool CheckHasDefaultValue(ParameterInfo parameter, out bool tryToGetDefaultValue) + { + 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; + } + } + } +} \ No newline at end of file diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs index 94bbfdb..99ef9ae 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 @@ namespace Microsoft.Extensions.DependencyInjection 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 c6eff6e..b275c71 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,18 +1,30 @@ - netstandard2.1;netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461 true enable Abstractions for dependency injection. Commonly Used Types: Microsoft.Extensions.DependencyInjection.IServiceCollection + + false + + + + + + + + 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 9884fdc..b93c0a8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/Microsoft.Extensions.DependencyInjection.csproj @@ -1,13 +1,15 @@ - netstandard2.1;netstandard2.0;net461 + $(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461 False Annotations $(NoWarn);CP0001 Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection. + + false @@ -17,21 +19,29 @@ $(DefineConstants);SAVE_ASSEMBLIES + + + + + + + + + + + + - - - - - - -- 2.7.4