1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections.Generic;
6 using System.Composition.Hosting;
7 using System.Composition.Hosting.Core;
8 using System.Composition.TypedParts;
9 using System.Composition.TypedParts.Discovery;
10 using System.Composition.TypedParts.Util;
11 using System.Diagnostics;
12 using System.Reflection;
13 
14 namespace System.Composition.Debugging
15 {
16     internal class ContainerConfigurationDebuggerProxy
17     {
18         private readonly ContainerConfiguration _configuration;
19         private DiscoveredPart[] _discoveredParts;
20         private Type[] _ignoredTypes;
21 
ContainerConfigurationDebuggerProxy(ContainerConfiguration configuration)22         public ContainerConfigurationDebuggerProxy(ContainerConfiguration configuration)
23         {
24             _configuration = configuration;
25         }
26 
27         [DebuggerDisplay("Added Providers")]
28         public ExportDescriptorProvider[] AddedExportDescriptorProviders
29         {
30             get { return _configuration.DebugGetAddedExportDescriptorProviders(); }
31         }
32 
33         [DebuggerDisplay("Discovered Parts")]
34         public DiscoveredPart[] DiscoveredParts
35         {
36             get
37             {
38                 InitDiscovery();
39                 return _discoveredParts;
40             }
41         }
42 
43         [DebuggerDisplay("Ignored Types")]
44         public Type[] IgnoredTypes
45         {
46             get
47             {
48                 InitDiscovery();
49                 return _ignoredTypes;
50             }
51         }
52 
InitDiscovery()53         private void InitDiscovery()
54         {
55             if (_discoveredParts != null)
56                 return;
57 
58             var types = _configuration.DebugGetRegisteredTypes();
59             var defaultAttributeContext = _configuration.DebugGetDefaultAttributeContext() ?? new DirectAttributeContext();
60             var discovered = new List<DiscoveredPart>();
61             var ignored = new List<Type>();
62 
63             foreach (var typeSet in types)
64             {
65                 var ac = typeSet.Item2 ?? defaultAttributeContext;
66                 var activationFeatures = TypedPartExportDescriptorProvider.DebugGetActivationFeatures(ac);
67                 var inspector = new TypeInspector(ac, activationFeatures);
68 
69                 foreach (var type in typeSet.Item1)
70                 {
71                     DiscoveredPart part;
72                     if (inspector.InspectTypeForPart(type.GetTypeInfo(), out part))
73                         discovered.Add(part);
74                     else
75                         ignored.Add(type);
76                 }
77             }
78 
79             _discoveredParts = discovered.ToArray();
80             _ignoredTypes = ignored.ToArray();
81         }
82     }
83 }
84