(ConstructorInfo, object[]) SelectConstructor(Type implType, Type genericsElementType, DiagnosticServiceProvider provider) { var constructors = implType.GetConstructors().OrderByDescending(x => x.GetParameters().Length); ConstructorInfo ctor = default; object[] args = default; foreach (var item in constructors) { ctor = item; var parameters = ctor.GetParameters(); args = parameters.Select(x => provider.GetServiceCore1(x.ParameterType, genericsElementType)).ToArray(); if (args.Any(x => x == null)) { continue; } break; } return(ctor, args); }
public object Instantiate(DiagnosticServiceProvider provider, Type genericsElementType) { if (descriptor.Lifetime == ServiceLifetime.Singleton) { if (isGenericType && genericsElementType != null) { if (ItemPerGenerics.TryGetValue(genericsElementType, out var value)) { return(value); } } else { if (Item != null) { return(Item); } } } lock (this) { object instance; if (descriptor.ImplementationFactory != null) { instance = descriptor.ImplementationFactory(provider); } else { var implType = descriptor.ImplementationType; if (isGenericType && genericsElementType != null) { implType = implType.MakeGenericType(genericsElementType); } var(ctor, args) = SelectConstructor(implType, genericsElementType, provider); if (ctor == null) { throw new InvalidOperationException("IL2CPP issue, ctor is null. Type:" + implType); } instance = ctor.Invoke(args); } if (descriptor.Lifetime == ServiceLifetime.Singleton) { if (isGenericType && genericsElementType != null) { ItemPerGenerics[genericsElementType] = instance; } else { Item = instance; } return(instance); } else { // Transient, Scoped(not supported, same as Transient). return(instance); } } }