Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BaseProxyService.cs « TextUIUtil « Util « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 006299085c7068e77f94643462b5945531ecea31 (plain)
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
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;

namespace Microsoft.VisualStudio.Utilities
{
    /// <summary>
    /// A proxy service for exposing best implementation to the MEF composition.
    /// </summary>
    internal abstract class BaseProxyService<T> where T : class
    {
        protected abstract IEnumerable<Lazy<T, IOrderable>> UnorderedImplementations { get; set; }

        private T bestImpl;

        protected virtual T BestImplementation
        {
            get
            {
                if (this.bestImpl == null)
                {
                    var orderedImpls = Orderer.Order(UnorderedImplementations);
                    if (orderedImpls.Count == 0)
                    {
                        throw new ImportCardinalityMismatchException($"Expected to import at least one export of {typeof(T).FullName}, but got none.");
                    }

                    this.bestImpl = orderedImpls[0].Value;
                }

                return this.bestImpl;
            }
        }
    }
}