/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/LanguageServer/Protocol/LspServices/BaseService.cs
50 строк
2 KB
Tomáš Matoušek
Move LanguageServer out of Features directory (#73779)
30 май 2024, 18:49
Не верифицирован
30 май 2024, 18:49
12f8968
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using Microsoft.CommonLanguageServerProtocol.Framework; namespace Microsoft.CodeAnalysis.LanguageServer; /// <summary> /// Simple helper class to hold a service instance or a lazily-created service. /// </summary> internal abstract class BaseService { public abstract Type Type { get; } public abstract object GetInstance(ILspServices lspServices); public static BaseService Create<T>(T instance) where T : class => new ConcreteService<T>(instance); public static BaseService CreateLazily<T>(Func<ILspServices, T> creator) where T : class => new LazyService<T>(creator); private sealed class ConcreteService<T>(T instance) : BaseService where T : class { public override Type Type => typeof(T); public override object GetInstance(ILspServices lspServices) => instance; } private sealed class LazyService<T>(Func<ILspServices, T> creator) : BaseService where T : class { private readonly object _gate = new(); private readonly Func<ILspServices, T> _creator = creator; private T? _instance; public override Type Type => typeof(T); public override object GetInstance(ILspServices lspServices) { lock (_gate) { return _instance ??= _creator(lspServices); } } } }