Skip to content

Commit

Permalink
Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant (#56324)
Browse files Browse the repository at this point in the history
* 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 <viktor.hofer@microsoft.com>

Co-authored-by: Viktor Hofer <viktor.hofer@microsoft.com>
  • Loading branch information
maryamariyan and ViktorHofer committed Jul 30, 2021
1 parent 7060801 commit 222c613
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,13 @@

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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Func<IServiceProvider, object[], object>>(
var factoryLambda = Expression.Lambda<Func<IServiceProvider, object?[]?, object>>(
factoryExpressionBody, provider, argumentArray);

Func<IServiceProvider, object[], object>? result = factoryLambda.Compile();
Func<IServiceProvider, object?[]?, object>? result = factoryLambda.Compile();
return result.Invoke;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<Nullable>enable</Nullable>
<PackageDescription>Abstractions for dependency injection.

Commonly Used Types:
Microsoft.Extensions.DependencyInjection.IServiceCollection</PackageDescription>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netstandard.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netstandard.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.1;netstandard2.0;net461</TargetFrameworks>
<!-- Debug IL generation -->
<ILEmitBackendSaveAssemblies>False</ILEmitBackendSaveAssemblies>
<Nullable>Annotations</Nullable>
<!-- Type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' has been forwaded down.-->
<NoWarn>$(NoWarn);CP0001</NoWarn>
<PackageDescription>Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection.</PackageDescription>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
Expand All @@ -17,21 +19,29 @@
<DefineConstants Condition="$(TargetFramework.StartsWith('net4')) and '$(ILEmitBackendSaveAssemblies)' == 'True'">$(DefineConstants);SAVE_ASSEMBLIES</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netstandard.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netstandard.cs" />
<!-- These types weren't available before net5.0 and need to be compiled into the assembly. -->
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<Compile Remove="ServiceLookup\ILEmit\**\*.cs" />

<Compile Condition="'$(ILEmitBackend)' == 'True'" Include="ServiceLookup\ILEmit\*.cs" />

<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" />
<Compile Include="$(CommonPath)Extensions\TypeNameHelper\TypeNameHelper.cs"
Link="Common\src\Extensions\TypeNameHelper\TypeNameHelper.cs" />

<!-- These types weren't available before net5.0 and need to be compiled into the assembly. -->
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\DynamicallyAccessedMemberTypes.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\UnconditionalSuppressMessageAttribute.cs" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 222c613

Please sign in to comment.