1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Shell;
namespace QtVsTools.VisualStudio
{
using ServiceType = Tuple<Type, Type>;
public interface IVsServiceProvider
{
I GetService<T, I>() where T : class where I : class;
Task<I> GetServiceAsync<T, I>() where T : class where I : class;
}
public static class VsServiceProvider
{
public static IVsServiceProvider Instance { get; set; }
static readonly ConcurrentDictionary<ServiceType, object> services = new();
public static I GetService<I>()
where I : class
{
return GetService<I, I>();
}
public static I GetService<T, I>()
where T : class
where I : class
{
if (Instance == null)
return null;
if (services.TryGetValue(new ServiceType(typeof(T), typeof(I)), out object serviceObj))
return serviceObj as I;
var serviceInterface = Instance.GetService<T, I>();
services.TryAdd(new ServiceType(typeof(T), typeof(I)), serviceInterface);
return serviceInterface;
}
public static async Task<I> GetServiceAsync<I>()
where I : class
{
return await GetServiceAsync<I, I>();
}
public static async Task<I> GetServiceAsync<T, I>()
where T : class
where I : class
{
if (Instance == null)
return null;
if (services.TryGetValue(new ServiceType(typeof(T), typeof(I)), out object serviceObj))
return serviceObj as I;
var serviceInterface = await Instance.GetServiceAsync<T, I>();
services.TryAdd(new ServiceType(typeof(T), typeof(I)), serviceInterface);
return serviceInterface;
}
public static I GetGlobalService<T, I>()
where T : class
where I : class
{
return Package.GetGlobalService(typeof(T)) as I;
}
}
}
|