ActivatorUtilities
Helper code for the various activator services.
using Microsoft.Extensions.Internal;
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Microsoft.Extensions.DependencyInjection
{
[NullableContext(1)]
[Nullable(0)]
public static class ActivatorUtilities
{
private readonly struct FactoryParameterContext
{
public Type ParameterType { get; }
public bool HasDefaultValue { get; }
public object DefaultValue { get; }
public int ArgumentIndex { get; }
public object ServiceKey { get; }
public FactoryParameterContext(Type parameterType, bool hasDefaultValue, object defaultValue, int argumentIndex, object serviceKey)
{
ParameterType = parameterType;
HasDefaultValue = hasDefaultValue;
DefaultValue = defaultValue;
ArgumentIndex = argumentIndex;
ServiceKey = serviceKey;
}
}
private sealed class ConstructorInfoEx
{
public readonly ConstructorInfo Info;
public readonly ParameterInfo[] Parameters;
public readonly bool IsPreferred;
private readonly object[] _parameterKeys;
public ConstructorInvoker _invoker;
public ConstructorInvoker Invoker {
get {
if (_invoker == null)
_invoker = ConstructorInvoker.Create(Info);
return _invoker;
}
}
public ConstructorInfoEx(ConstructorInfo constructor)
{
Info = constructor;
Parameters = constructor.GetParameters();
IsPreferred = constructor.IsDefined(typeof(ActivatorUtilitiesConstructorAttribute), false);
for (int i = 0; i < Parameters.Length; i++) {
FromKeyedServicesAttribute fromKeyedServicesAttribute = (FromKeyedServicesAttribute)Attribute.GetCustomAttribute(Parameters[i], typeof(FromKeyedServicesAttribute), false);
if (fromKeyedServicesAttribute != null) {
if (_parameterKeys == null)
_parameterKeys = new object[Parameters.Length];
_parameterKeys[i] = fromKeyedServicesAttribute.Key;
}
}
}
public bool IsService(IServiceProviderIsService serviceProviderIsService, int parameterIndex)
{
ParameterInfo parameterInfo = Parameters[parameterIndex];
object[] parameterKeys = _parameterKeys;
object obj = (parameterKeys != null) ? parameterKeys[parameterIndex] : null;
if (obj != null) {
IServiceProviderIsKeyedService serviceProviderIsKeyedService = serviceProviderIsService as IServiceProviderIsKeyedService;
if (serviceProviderIsKeyedService != null)
return serviceProviderIsKeyedService.IsKeyedService(parameterInfo.ParameterType, obj);
throw new InvalidOperationException(System.SR.KeyedServicesNotSupported);
}
return serviceProviderIsService.IsService(parameterInfo.ParameterType);
}
public object GetService(IServiceProvider serviceProvider, int parameterIndex)
{
ParameterInfo parameterInfo = Parameters[parameterIndex];
object[] parameterKeys = _parameterKeys;
object obj = (parameterKeys != null) ? parameterKeys[parameterIndex] : null;
if (obj != null) {
IKeyedServiceProvider keyedServiceProvider = serviceProvider as IKeyedServiceProvider;
if (keyedServiceProvider != null)
return keyedServiceProvider.GetKeyedService(parameterInfo.ParameterType, obj);
throw new InvalidOperationException(System.SR.KeyedServicesNotSupported);
}
return serviceProvider.GetService(parameterInfo.ParameterType);
}
}
[CompilerFeatureRequired("RefStructs")]
private readonly ref struct ConstructorMatcher
{
private readonly ConstructorInfoEx _constructor;
private readonly Span<object> _parameterValues;
public ConstructorInfoEx ConstructorInfo => _constructor;
public ConstructorMatcher(ConstructorInfoEx constructor, Span<object> parameterValues)
{
_constructor = constructor;
_parameterValues = parameterValues;
}
public int Match(object[] givenParameters, IServiceProviderIsService serviceProviderIsService)
{
for (int i = 0; i < givenParameters.Length; i++) {
Type c = givenParameters[i]?.GetType();
bool flag = false;
for (int j = 0; j < _constructor.Parameters.Length; j++) {
if (_parameterValues[j] == null && _constructor.Parameters[j].ParameterType.IsAssignableFrom(c)) {
flag = true;
_parameterValues[j] = givenParameters[i];
break;
}
}
if (!flag)
return -1;
}
for (int k = 0; k < _constructor.Parameters.Length; k++) {
if (_parameterValues[k] == null && !_constructor.IsService(serviceProviderIsService, k)) {
if (!ParameterDefaultValue.TryGetDefaultValue(_constructor.Parameters[k], out object defaultValue))
return -1;
_parameterValues[k] = defaultValue;
}
}
return _constructor.Parameters.Length;
}
public object CreateInstance(IServiceProvider provider)
{
for (int i = 0; i < _constructor.Parameters.Length; i++) {
if (_parameterValues[i] == null) {
object service = _constructor.GetService(provider, i);
if (service == null) {
if (!ParameterDefaultValue.TryGetDefaultValue(_constructor.Parameters[i], out object defaultValue))
throw new InvalidOperationException(System.SR.Format(System.SR.UnableToResolveService, _constructor.Parameters[i].ParameterType, _constructor.Info.DeclaringType));
_parameterValues[i] = defaultValue;
} else
_parameterValues[i] = service;
}
}
return _constructor.Invoker.Invoke(_parameterValues.Slice(0, _constructor.Parameters.Length));
}
public void MapParameters(int?[] parameterMap, object[] givenParameters)
{
for (int i = 0; i < _constructor.Parameters.Length; i++) {
if (parameterMap[i].HasValue)
_parameterValues[i] = givenParameters[parameterMap[i].Value];
}
}
}
[NullableContext(0)]
internal static class ActivatorUtilitiesUpdateHandler
{
public static void ClearCache([Nullable(new byte[] {
2,
1
})] Type[] _)
{
s_constructorInfos.Clear();
if (s_collectibleConstructorInfos.IsValueCreated)
s_collectibleConstructorInfos.Value.Clear();
}
}
[InlineArray(8)]
private struct StackAllocatedObjects
{
internal const int MaxStackAllocArgCount = 8;
private object _arg0;
}
private static readonly ConcurrentDictionary<Type, ConstructorInfoEx[]> s_constructorInfos = new ConcurrentDictionary<Type, ConstructorInfoEx[]>();
private static readonly Lazy<ConditionalWeakTable<Type, ConstructorInfoEx[]>> s_collectibleConstructorInfos = new Lazy<ConditionalWeakTable<Type, ConstructorInfoEx[]>>();
private const int FixedArgumentThreshold = 4;
private static readonly MethodInfo GetServiceInfo = new Func<IServiceProvider, Type, Type, bool, object, object>(GetService).Method;
public static object CreateInstance(IServiceProvider provider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, params object[] parameters)
{
if (provider == null)
throw new ArgumentNullException("provider");
if (instanceType.IsAbstract)
throw new InvalidOperationException(System.SR.CannotCreateAbstractClasses);
<>c__DisplayClass4_0 <>c__DisplayClass4_ = default(<>c__DisplayClass4_0);
if (!s_constructorInfos.TryGetValue(instanceType, out <>c__DisplayClass4_.constructors))
<>c__DisplayClass4_.constructors = GetOrAddConstructors(instanceType);
StackAllocatedObjects buffer = default(StackAllocatedObjects);
int num = <CreateInstance>g__GetMaxArgCount|4_0(ref <>c__DisplayClass4_);
Span<object> span = (num <= 4) ? global::<PrivateImplementationDetails>.InlineArrayAsSpan<StackAllocatedObjects, object>(ref buffer, 8) : ((Span<object>)new object[num * 2]);
Span<object> ctorArgs = span.Slice(0, num);
Span<object> span2 = span.Slice(num, num);
ConstructorMatcher constructorMatcher = default(ConstructorMatcher);
IServiceProviderIsService service = provider.GetService<IServiceProviderIsService>();
ConstructorInfoEx constructorInfoEx;
if (service != null) {
for (int i = 0; i < <>c__DisplayClass4_.constructors.Length; i++) {
constructorInfoEx = <>c__DisplayClass4_.constructors[i];
if (constructorInfoEx.IsPreferred) {
for (int j = i + 1; j < <>c__DisplayClass4_.constructors.Length; j++) {
if (<>c__DisplayClass4_.constructors[j].IsPreferred)
ThrowMultipleCtorsMarkedWithAttributeException();
}
<CreateInstance>g__InitializeCtorArgValues|4_1(ref ctorArgs, constructorInfoEx.Parameters.Length);
constructorMatcher = new ConstructorMatcher(constructorInfoEx, ctorArgs);
if (constructorMatcher.Match(parameters, service) == -1)
ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
return constructorMatcher.CreateInstance(provider);
}
}
int num2 = -1;
ConstructorMatcher constructorMatcher2 = default(ConstructorMatcher);
bool flag = false;
for (int k = 0; k < <>c__DisplayClass4_.constructors.Length; k++) {
constructorInfoEx = <>c__DisplayClass4_.constructors[k];
<CreateInstance>g__InitializeCtorArgValues|4_1(ref ctorArgs, constructorInfoEx.Parameters.Length);
constructorMatcher = new ConstructorMatcher(constructorInfoEx, ctorArgs);
int num3 = constructorMatcher.Match(parameters, service);
if (num2 < num3) {
num2 = num3;
ctorArgs.CopyTo(span2);
constructorMatcher2 = new ConstructorMatcher(constructorMatcher.ConstructorInfo, span2);
flag = false;
} else if (num2 == num3) {
flag = true;
}
}
if (num2 != -1) {
if (flag)
throw new InvalidOperationException(System.SR.Format(System.SR.MultipleCtorsFoundWithBestLength, instanceType, num2));
return constructorMatcher2.CreateInstance(provider);
}
}
Type[] array;
if (parameters.Length == 0)
array = Type.EmptyTypes;
else {
array = new Type[parameters.Length];
for (int l = 0; l < array.Length; l++) {
array[l] = parameters[l]?.GetType();
}
}
FindApplicableConstructor(instanceType, array, <>c__DisplayClass4_.constructors, out ConstructorInfo matchingConstructor, out int?[] matchingParameterMap);
constructorInfoEx = FindConstructorEx(matchingConstructor, <>c__DisplayClass4_.constructors);
<CreateInstance>g__InitializeCtorArgValues|4_1(ref ctorArgs, constructorInfoEx.Parameters.Length);
constructorMatcher = new ConstructorMatcher(constructorInfoEx, ctorArgs);
constructorMatcher.MapParameters(matchingParameterMap, parameters);
return constructorMatcher.CreateInstance(provider);
}
private static ConstructorInfoEx[] GetOrAddConstructors([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
if (!type.Assembly.IsCollectible)
return s_constructorInfos.GetOrAdd(type, CreateConstructorInfoExs(type));
if (s_collectibleConstructorInfos.Value.TryGetValue(type, out ConstructorInfoEx[] value))
return value;
value = CreateConstructorInfoExs(type);
s_collectibleConstructorInfos.Value.AddOrUpdate(type, value);
return value;
}
private static ConstructorInfoEx[] CreateConstructorInfoExs([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
ConstructorInfo[] constructors = type.GetConstructors();
ConstructorInfoEx[] array = new ConstructorInfoEx[constructors.Length];
for (int i = 0; i < constructors.Length; i++) {
array[i] = new ConstructorInfoEx(constructors[i]);
}
return array;
}
public static ObjectFactory CreateFactory([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes)
{
if (!RuntimeFeature.IsDynamicCodeCompiled)
return CreateFactoryReflection(instanceType, argumentTypes);
CreateFactoryInternal(instanceType, argumentTypes, out ParameterExpression provider, out ParameterExpression argumentArray, out Expression factoryExpressionBody);
return Expression.Lambda<Func<IServiceProvider, object[], object>>(factoryExpressionBody, new ParameterExpression[2] {
provider,
argumentArray
}).Compile().Invoke;
}
public static ObjectFactory<T> CreateFactory<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(Type[] argumentTypes)
{
if (!RuntimeFeature.IsDynamicCodeCompiled) {
ObjectFactory factory = CreateFactoryReflection(typeof(T), argumentTypes);
return (IServiceProvider serviceProvider, object[] arguments) => (T)factory(serviceProvider, arguments);
}
CreateFactoryInternal(typeof(T), argumentTypes, out ParameterExpression provider, out ParameterExpression argumentArray, out Expression factoryExpressionBody);
return Expression.Lambda<Func<IServiceProvider, object[], T>>(factoryExpressionBody, new ParameterExpression[2] {
provider,
argumentArray
}).Compile().Invoke;
}
private static void CreateFactoryInternal([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes, out ParameterExpression provider, out ParameterExpression argumentArray, out Expression factoryExpressionBody)
{
FindApplicableConstructor(instanceType, argumentTypes, null, out ConstructorInfo matchingConstructor, out int?[] matchingParameterMap);
provider = Expression.Parameter(typeof(IServiceProvider), "provider");
argumentArray = Expression.Parameter(typeof(object[]), "argumentArray");
factoryExpressionBody = BuildFactoryExpression(matchingConstructor, matchingParameterMap, provider, argumentArray);
}
public static T CreateInstance<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(IServiceProvider provider, params object[] parameters)
{
return (T)CreateInstance(provider, typeof(T), parameters);
}
public static T GetServiceOrCreateInstance<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(IServiceProvider provider)
{
return (T)GetServiceOrCreateInstance(provider, typeof(T));
}
public static object GetServiceOrCreateInstance(IServiceProvider provider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
{
return provider.GetService(type) ?? CreateInstance(provider, type, Array.Empty<object>());
}
private static object GetService(IServiceProvider sp, Type type, Type requiredBy, bool hasDefaultValue, object key)
{
object obj = (key == null) ? sp.GetService(type) : GetKeyedService(sp, type, key);
if (obj == null && !hasDefaultValue)
ThrowHelperUnableToResolveService(type, requiredBy);
return obj;
}
[DoesNotReturn]
private static void ThrowHelperUnableToResolveService(Type type, Type requiredBy)
{
throw new InvalidOperationException(System.SR.Format(System.SR.UnableToResolveService, type, requiredBy));
}
private static BlockExpression BuildFactoryExpression(ConstructorInfo constructor, int?[] parameterMap, Expression serviceProvider, Expression factoryArgumentArray)
{
ParameterInfo[] parameters = constructor.GetParameters();
Expression[] array = new Expression[parameters.Length];
for (int i = 0; i < parameters.Length; i++) {
ParameterInfo parameterInfo = parameters[i];
Type parameterType = parameterInfo.ParameterType;
object defaultValue;
bool flag = ParameterDefaultValue.TryGetDefaultValue(parameterInfo, out defaultValue);
if (parameterMap[i].HasValue)
array[i] = Expression.ArrayAccess(factoryArgumentArray, Expression.Constant(parameterMap[i]));
else {
FromKeyedServicesAttribute fromKeyedServicesAttribute = (FromKeyedServicesAttribute)Attribute.GetCustomAttribute(parameterInfo, typeof(FromKeyedServicesAttribute), false);
Expression[] arguments = new Expression[5] {
serviceProvider,
Expression.Constant(parameterType, typeof(Type)),
Expression.Constant(constructor.DeclaringType, typeof(Type)),
Expression.Constant(flag),
Expression.Constant(fromKeyedServicesAttribute?.Key, typeof(object))
};
array[i] = Expression.Call(GetServiceInfo, arguments);
}
if (flag) {
ConstantExpression right = Expression.Constant(defaultValue);
array[i] = Expression.Coalesce(array[i], right);
}
array[i] = Expression.Convert(array[i], parameterType);
}
return Expression.Block(Expression.IfThen(Expression.Equal(serviceProvider, Expression.Constant(null)), Expression.Throw(Expression.Constant(new ArgumentNullException("serviceProvider")))), Expression.New(constructor, array));
}
[DoesNotReturn]
private static void ThrowHelperArgumentNullExceptionServiceProvider()
{
throw new ArgumentNullException("serviceProvider");
}
private static ObjectFactory CreateFactoryReflection([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes)
{
FindApplicableConstructor(instanceType, argumentTypes, null, out ConstructorInfo matchingConstructor, out int?[] parameterMap);
Type declaringType = matchingConstructor.DeclaringType;
ConstructorInvoker invoker = ConstructorInvoker.Create(matchingConstructor);
ParameterInfo[] constructorParameters = matchingConstructor.GetParameters();
if (constructorParameters.Length == 0)
return (IServiceProvider serviceProvider, object[] arguments) => invoker.Invoke();
bool useFixedValues = constructorParameters.Length <= 4;
bool flag = false;
int num = 0;
int num2 = 0;
for (int i = 0; i < constructorParameters.Length; i++) {
flag |= constructorParameters[i].HasDefaultValue;
int? nullable = parameterMap[i];
if (nullable.HasValue) {
num++;
nullable = parameterMap[i];
int num3 = i;
if ((nullable.GetValueOrDefault() == num3) & nullable.HasValue)
num2++;
}
}
<>c__DisplayClass17_0 <>c__DisplayClass17_;
if (flag || num != num2)
return <>c__DisplayClass17_.<CreateFactoryReflection>g__InvokeCanonical|2();
if (num == 0) {
<>c__DisplayClass17_0 <>c__DisplayClass17_2;
FactoryParameterContext[] parameters = <>c__DisplayClass17_2.<CreateFactoryReflection>g__GetFactoryParameterContext|3();
if (!useFixedValues)
return (IServiceProvider serviceProvider, object[] arguments) => ReflectionFactoryServiceOnlySpan(invoker, parameters, declaringType, serviceProvider);
return (IServiceProvider serviceProvider, object[] arguments) => ReflectionFactoryServiceOnlyFixed(invoker, parameters, declaringType, serviceProvider);
}
if (num == constructorParameters.Length)
return (IServiceProvider serviceProvider, object[] arguments) => ReflectionFactoryDirect(invoker, serviceProvider, arguments);
return <>c__DisplayClass17_.<CreateFactoryReflection>g__InvokeCanonical|2();
}
private static void FindApplicableConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes, ConstructorInfoEx[] constructors, out ConstructorInfo matchingConstructor, out int?[] matchingParameterMap)
{
if (!TryFindPreferredConstructor(instanceType, argumentTypes, constructors, out ConstructorInfo matchingConstructor2, out int?[] parameterMap) && !TryFindMatchingConstructor(instanceType, argumentTypes, out matchingConstructor2, out parameterMap))
throw new InvalidOperationException(System.SR.Format(System.SR.CtorNotLocated, instanceType));
matchingConstructor = matchingConstructor2;
matchingParameterMap = parameterMap;
}
private static ConstructorInfoEx FindConstructorEx(ConstructorInfo constructorInfo, ConstructorInfoEx[] constructorExs)
{
for (int i = 0; i < constructorExs.Length; i++) {
if ((object)constructorExs[i].Info == constructorInfo)
return constructorExs[i];
}
return null;
}
private static bool TryFindMatchingConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes, [NotNullWhen(true)] out ConstructorInfo matchingConstructor, [NotNullWhen(true)] out int?[] parameterMap)
{
matchingConstructor = null;
parameterMap = null;
ConstructorInfo[] constructors = instanceType.GetConstructors();
foreach (ConstructorInfo constructorInfo in constructors) {
if (TryCreateParameterMap(constructorInfo.GetParameters(), argumentTypes, out int?[] parameterMap2)) {
if (matchingConstructor != (ConstructorInfo)null)
throw new InvalidOperationException(System.SR.Format(System.SR.MultipleCtorsFound, instanceType));
matchingConstructor = constructorInfo;
parameterMap = parameterMap2;
}
}
if (matchingConstructor != (ConstructorInfo)null)
return true;
return false;
}
private static bool TryFindPreferredConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, Type[] argumentTypes, ConstructorInfoEx[] constructors, [NotNullWhen(true)] out ConstructorInfo matchingConstructor, [NotNullWhen(true)] out int?[] parameterMap)
{
bool flag = false;
matchingConstructor = null;
parameterMap = null;
if (constructors == null && !s_constructorInfos.TryGetValue(instanceType, out constructors))
constructors = GetOrAddConstructors(instanceType);
ConstructorInfoEx[] array = constructors;
foreach (ConstructorInfoEx constructorInfoEx in array) {
if (constructorInfoEx.IsPreferred) {
if (flag)
ThrowMultipleCtorsMarkedWithAttributeException();
if (!TryCreateParameterMap(constructorInfoEx.Info.GetParameters(), argumentTypes, out int?[] parameterMap2))
ThrowMarkedCtorDoesNotTakeAllProvidedArguments();
matchingConstructor = constructorInfoEx.Info;
parameterMap = parameterMap2;
flag = true;
}
}
if (matchingConstructor != (ConstructorInfo)null)
return true;
return false;
}
private static bool TryCreateParameterMap(ParameterInfo[] constructorParameters, Type[] argumentTypes, out int?[] parameterMap)
{
parameterMap = new int?[constructorParameters.Length];
for (int i = 0; i < argumentTypes.Length; i++) {
bool flag = false;
Type c = argumentTypes[i];
for (int j = 0; j < constructorParameters.Length; j++) {
if (!parameterMap[j].HasValue && constructorParameters[j].ParameterType.IsAssignableFrom(c)) {
flag = true;
parameterMap[j] = i;
break;
}
}
if (!flag)
return false;
}
return true;
}
private static void ThrowMultipleCtorsMarkedWithAttributeException()
{
throw new InvalidOperationException(System.SR.Format(System.SR.MultipleCtorsMarkedWithAttribute, "ActivatorUtilitiesConstructorAttribute"));
}
private static void ThrowMarkedCtorDoesNotTakeAllProvidedArguments()
{
throw new InvalidOperationException(System.SR.Format(System.SR.MarkedCtorMissingArgumentTypes, "ActivatorUtilitiesConstructorAttribute"));
}
private static object ReflectionFactoryServiceOnlyFixed(ConstructorInvoker invoker, FactoryParameterContext[] parameters, Type declaringType, IServiceProvider serviceProvider)
{
if (serviceProvider == null)
ThrowHelperArgumentNullExceptionServiceProvider();
switch (parameters.Length) {
case 1:
return invoker.Invoke(GetService(serviceProvider, parameters[0].ParameterType, declaringType, false, parameters[0].ServiceKey));
case 2:
return invoker.Invoke(GetService(serviceProvider, parameters[0].ParameterType, declaringType, false, parameters[0].ServiceKey), GetService(serviceProvider, parameters[1].ParameterType, declaringType, false, parameters[1].ServiceKey));
case 3:
return invoker.Invoke(GetService(serviceProvider, parameters[0].ParameterType, declaringType, false, parameters[0].ServiceKey), GetService(serviceProvider, parameters[1].ParameterType, declaringType, false, parameters[1].ServiceKey), GetService(serviceProvider, parameters[2].ParameterType, declaringType, false, parameters[2].ServiceKey));
case 4:
return invoker.Invoke(GetService(serviceProvider, parameters[0].ParameterType, declaringType, false, parameters[0].ServiceKey), GetService(serviceProvider, parameters[1].ParameterType, declaringType, false, parameters[1].ServiceKey), GetService(serviceProvider, parameters[2].ParameterType, declaringType, false, parameters[2].ServiceKey), GetService(serviceProvider, parameters[3].ParameterType, declaringType, false, parameters[3].ServiceKey));
default:
return null;
}
}
private static object ReflectionFactoryServiceOnlySpan(ConstructorInvoker invoker, FactoryParameterContext[] parameters, Type declaringType, IServiceProvider serviceProvider)
{
if (serviceProvider == null)
ThrowHelperArgumentNullExceptionServiceProvider();
object[] array = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++) {
array[i] = GetService(serviceProvider, parameters[i].ParameterType, declaringType, false, parameters[i].ServiceKey);
}
return invoker.Invoke(array.AsSpan());
}
private static object ReflectionFactoryCanonicalFixed(ConstructorInvoker invoker, FactoryParameterContext[] parameters, Type declaringType, IServiceProvider serviceProvider, object[] arguments)
{
if (serviceProvider == null)
ThrowHelperArgumentNullExceptionServiceProvider();
ref FactoryParameterContext reference = ref parameters[0];
switch (parameters.Length) {
case 1:
return invoker.Invoke(((reference.ArgumentIndex != -1) ? arguments[reference.ArgumentIndex] : GetService(serviceProvider, reference.ParameterType, declaringType, reference.HasDefaultValue, reference.ServiceKey)) ?? reference.DefaultValue);
case 2: {
ref FactoryParameterContext reference7 = ref parameters[1];
return invoker.Invoke(((reference.ArgumentIndex != -1) ? arguments[reference.ArgumentIndex] : GetService(serviceProvider, reference.ParameterType, declaringType, reference.HasDefaultValue, reference.ServiceKey)) ?? reference.DefaultValue, ((reference7.ArgumentIndex != -1) ? arguments[reference7.ArgumentIndex] : GetService(serviceProvider, reference7.ParameterType, declaringType, reference7.HasDefaultValue, reference7.ServiceKey)) ?? reference7.DefaultValue);
}
case 3: {
ref FactoryParameterContext reference5 = ref parameters[1];
ref FactoryParameterContext reference6 = ref parameters[2];
return invoker.Invoke(((reference.ArgumentIndex != -1) ? arguments[reference.ArgumentIndex] : GetService(serviceProvider, reference.ParameterType, declaringType, reference.HasDefaultValue, reference.ServiceKey)) ?? reference.DefaultValue, ((reference5.ArgumentIndex != -1) ? arguments[reference5.ArgumentIndex] : GetService(serviceProvider, reference5.ParameterType, declaringType, reference5.HasDefaultValue, reference5.ServiceKey)) ?? reference5.DefaultValue, ((reference6.ArgumentIndex != -1) ? arguments[reference6.ArgumentIndex] : GetService(serviceProvider, reference6.ParameterType, declaringType, reference6.HasDefaultValue, reference6.ServiceKey)) ?? reference6.DefaultValue);
}
case 4: {
ref FactoryParameterContext reference2 = ref parameters[1];
ref FactoryParameterContext reference3 = ref parameters[2];
ref FactoryParameterContext reference4 = ref parameters[3];
return invoker.Invoke(((reference.ArgumentIndex != -1) ? arguments[reference.ArgumentIndex] : GetService(serviceProvider, reference.ParameterType, declaringType, reference.HasDefaultValue, reference.ServiceKey)) ?? reference.DefaultValue, ((reference2.ArgumentIndex != -1) ? arguments[reference2.ArgumentIndex] : GetService(serviceProvider, reference2.ParameterType, declaringType, reference2.HasDefaultValue, reference2.ServiceKey)) ?? reference2.DefaultValue, ((reference3.ArgumentIndex != -1) ? arguments[reference3.ArgumentIndex] : GetService(serviceProvider, reference3.ParameterType, declaringType, reference3.HasDefaultValue, reference3.ServiceKey)) ?? reference3.DefaultValue, ((reference4.ArgumentIndex != -1) ? arguments[reference4.ArgumentIndex] : GetService(serviceProvider, reference4.ParameterType, declaringType, reference4.HasDefaultValue, reference4.ServiceKey)) ?? reference4.DefaultValue);
}
default:
return null;
}
}
private static object ReflectionFactoryCanonicalSpan(ConstructorInvoker invoker, FactoryParameterContext[] parameters, Type declaringType, IServiceProvider serviceProvider, object[] arguments)
{
if (serviceProvider == null)
ThrowHelperArgumentNullExceptionServiceProvider();
object[] array = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++) {
ref FactoryParameterContext reference = ref parameters[i];
array[i] = (((reference.ArgumentIndex != -1) ? arguments[reference.ArgumentIndex] : GetService(serviceProvider, reference.ParameterType, declaringType, reference.HasDefaultValue, reference.ServiceKey)) ?? reference.DefaultValue);
}
return invoker.Invoke(array.AsSpan());
}
private static object ReflectionFactoryDirect(ConstructorInvoker invoker, IServiceProvider serviceProvider, object[] arguments)
{
if (serviceProvider == null)
ThrowHelperArgumentNullExceptionServiceProvider();
if (arguments == null)
ThrowHelperNullReferenceException();
return invoker.Invoke(arguments.AsSpan());
}
[DoesNotReturn]
private static void ThrowHelperNullReferenceException()
{
throw new NullReferenceException();
}
private static object GetKeyedService(IServiceProvider provider, Type type, object serviceKey)
{
System.ThrowHelper.ThrowIfNull(provider, "provider");
IKeyedServiceProvider keyedServiceProvider = provider as IKeyedServiceProvider;
if (keyedServiceProvider != null)
return keyedServiceProvider.GetKeyedService(type, serviceKey);
throw new InvalidOperationException(System.SR.KeyedServicesNotSupported);
}
}
}