// Copyright (c) .NET Foundation. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt // in the project root for license information. // Modifications Copyright (c) Atlas Lift Tech Inc. All rights reserved. using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Concurrent; namespace MQTTnet.AspNetCore.AttributeRouting.Routing { /// /// Caches instances produced by . /// internal class TypeActivatorCache : ITypeActivatorCache { private readonly Func _createFactory = (type) => ActivatorUtilities.CreateFactory(type, Type.EmptyTypes); private readonly ConcurrentDictionary _typeActivatorCache = new ConcurrentDictionary(); /// public TInstance CreateInstance(IServiceProvider serviceProvider, Type implementationType) { if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } if (implementationType == null) { throw new ArgumentNullException(nameof(implementationType)); } var createFactory = _typeActivatorCache.GetOrAdd(implementationType, _createFactory); return (TInstance)createFactory(serviceProvider, arguments: null); } } }