/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Components/Shared/test/TestServiceProvider.cs
43 строки
1 KB
Mackinnon Buck
[release/8.0] [Blazor] Support injecting keyed services with `[Inject]` (#50561)
14 сен 2023, 02:31
Не верифицирован
14 сен 2023, 02:31
ad5ba5f
Код
Авторство
О чём код?
// 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 Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Components.Test.Helpers; public class TestServiceProvider : IServiceProvider, IKeyedServiceProvider { private readonly Dictionary<Type, Func<object?>> _factories = new(); private Dictionary<(Type, object?), Func<object?>>? _keyedFactories; private Dictionary<(Type, object?), Func<object?>> KeyedFactories { get { _keyedFactories ??= new(); return _keyedFactories; } } public object? GetService(Type serviceType) => _factories.TryGetValue(serviceType, out var factory) ? factory() : null; public object? GetKeyedService(Type serviceType, object? serviceKey) => KeyedFactories.TryGetValue((serviceType, serviceKey), out var factory) ? factory() : null; internal void AddService<T>(T value) => _factories.Add(typeof(T), () => value); internal void AddKeyedService<T>(T value, object serviceKey) => KeyedFactories.Add((typeof(T), serviceKey), () => value); public object GetRequiredKeyedService(Type serviceType, object? serviceKey) => throw new NotImplementedException(); }