Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ParameterDefaultValue.TryGetDefaultValue bitcode compliant #56324

Merged
8 commits merged into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Comment on lines +4 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these #nullable enables needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the file is shared between 2 projects - one that has been annotated for nullable, and one that hasn't.

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;
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
}

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;
}
}
}
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,7 +1,7 @@
<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.
Expand All @@ -10,9 +10,14 @@ Commonly Used Types:
Microsoft.Extensions.DependencyInjection.IServiceCollection</PackageDescription>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.cs"
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" />
<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs"
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.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 All @@ -24,4 +29,11 @@ Microsoft.Extensions.DependencyInjection.IServiceCollection</PackageDescription>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.AsyncInterfaces\src\Microsoft.Bcl.AsyncInterfaces.csproj" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">
<Reference Include="System.Collections" />
<Reference Include="System.ComponentModel" />
<Reference Include="System.Linq.Expressions" />
<Reference Include="System.Runtime" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<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>
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
<Nullable Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">enable</Nullable>
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
<!-- Type 'Microsoft.Extensions.DependencyInjection.ServiceCollection' has been forwaded down.-->
<NoWarn>$(NoWarn);CP0001</NoWarn>
<PackageDescription>Default implementation of dependency injection for Microsoft.Extensions.DependencyInjection.</PackageDescription>
Expand All @@ -17,21 +18,30 @@
<DefineConstants Condition="$(TargetFramework.StartsWith('net4')) and '$(ILEmitBackendSaveAssemblies)' == 'True'">$(DefineConstants);SAVE_ASSEMBLIES</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs"
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.netcoreapp.cs" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.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 All @@ -45,4 +55,20 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Bcl.AsyncInterfaces\src\Microsoft.Bcl.AsyncInterfaces.csproj" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp'">
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
<Reference Include="System.Collections" />
<Reference Include="System.Collections.Concurrent" />
<Reference Include="System.ComponentModel" />
<Reference Include="System.Diagnostics.Tracing" />
<Reference Include="System.Linq" />
<Reference Include="System.Linq.Expressions" />
<Reference Include="System.Reflection.Emit.ILGeneration" />
<Reference Include="System.Reflection.Emit.Lightweight" />
<Reference Include="System.Reflection.Primitives" />
<Reference Include="System.Runtime" />
<Reference Include="System.Threading" />
<Reference Include="System.Threading.ThreadPool" />
<Reference Include="netstandard" />
</ItemGroup>

</Project>