diff options
-rw-r--r-- | src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs | 20 | ||||
-rw-r--r-- | src/test/Example.Extension/ExampleExtensionFactory.cs | 22 |
2 files changed, 35 insertions, 7 deletions
diff --git a/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs b/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs index 5714701a..2ecf85ae 100644 --- a/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs +++ b/src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs | |||
@@ -16,14 +16,32 @@ namespace WixToolset.Core.ExtensibilityServices | |||
16 | private List<IExtensionFactory> extensionFactories = new List<IExtensionFactory>(); | 16 | private List<IExtensionFactory> extensionFactories = new List<IExtensionFactory>(); |
17 | private Dictionary<Type, List<object>> loadedExtensionsByType = new Dictionary<Type, List<object>>(); | 17 | private Dictionary<Type, List<object>> loadedExtensionsByType = new Dictionary<Type, List<object>>(); |
18 | 18 | ||
19 | public ExtensionManager(IServiceProvider serviceProvider) | ||
20 | { | ||
21 | this.ServiceProvider = serviceProvider; | ||
22 | } | ||
23 | |||
24 | private IServiceProvider ServiceProvider { get; } | ||
25 | |||
19 | public void Add(Assembly extensionAssembly) | 26 | public void Add(Assembly extensionAssembly) |
20 | { | 27 | { |
21 | var types = extensionAssembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && typeof(IExtensionFactory).IsAssignableFrom(t)); | 28 | var types = extensionAssembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && typeof(IExtensionFactory).IsAssignableFrom(t)); |
22 | var factories = types.Select(t => (IExtensionFactory)Activator.CreateInstance(t)).ToList(); | 29 | var factories = types.Select(this.CreateExtensionFactory).ToList(); |
23 | 30 | ||
24 | this.extensionFactories.AddRange(factories); | 31 | this.extensionFactories.AddRange(factories); |
25 | } | 32 | } |
26 | 33 | ||
34 | private IExtensionFactory CreateExtensionFactory(Type type) | ||
35 | { | ||
36 | var constructor = type.GetConstructor(new[] { typeof(IServiceProvider) }); | ||
37 | if (constructor != null) | ||
38 | { | ||
39 | return (IExtensionFactory)constructor.Invoke(new[] { this.ServiceProvider }); | ||
40 | } | ||
41 | |||
42 | return (IExtensionFactory)Activator.CreateInstance(type); | ||
43 | } | ||
44 | |||
27 | public void Load(string extensionPath) | 45 | public void Load(string extensionPath) |
28 | { | 46 | { |
29 | Assembly assembly; | 47 | Assembly assembly; |
diff --git a/src/test/Example.Extension/ExampleExtensionFactory.cs b/src/test/Example.Extension/ExampleExtensionFactory.cs index a081b758..ee9641a2 100644 --- a/src/test/Example.Extension/ExampleExtensionFactory.cs +++ b/src/test/Example.Extension/ExampleExtensionFactory.cs | |||
@@ -1,4 +1,4 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
2 | 2 | ||
3 | namespace Example.Extension | 3 | namespace Example.Extension |
4 | { | 4 | { |
@@ -9,16 +9,26 @@ namespace Example.Extension | |||
9 | { | 9 | { |
10 | private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension; | 10 | private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension; |
11 | 11 | ||
12 | public ExampleExtensionFactory(IServiceProvider serviceProvider) | ||
13 | { | ||
14 | this.ServiceProvider = serviceProvider; | ||
15 | } | ||
16 | |||
17 | /// <summary> | ||
18 | /// This exists just to show it is possible to get a service provider to the extension factory. | ||
19 | /// </summary> | ||
20 | private IServiceProvider ServiceProvider { get; } | ||
21 | |||
12 | public bool TryCreateExtension(Type extensionType, out object extension) | 22 | public bool TryCreateExtension(Type extensionType, out object extension) |
13 | { | 23 | { |
14 | if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension)) | 24 | if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension)) |
15 | { | 25 | { |
16 | if (preprocessorExtension == null) | 26 | if (this.preprocessorExtension == null) |
17 | { | 27 | { |
18 | preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); | 28 | this.preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); |
19 | } | 29 | } |
20 | 30 | ||
21 | extension = preprocessorExtension; | 31 | extension = this.preprocessorExtension; |
22 | } | 32 | } |
23 | else if (extensionType == typeof(ICompilerExtension)) | 33 | else if (extensionType == typeof(ICompilerExtension)) |
24 | { | 34 | { |
@@ -28,7 +38,7 @@ namespace Example.Extension | |||
28 | { | 38 | { |
29 | extension = new ExampleExtensionData(); | 39 | extension = new ExampleExtensionData(); |
30 | } | 40 | } |
31 | else if (extensionType == typeof(IWindowsInstallerBackendExtension)) | 41 | else if (extensionType == typeof(IWindowsInstallerBackendBinderExtension)) |
32 | { | 42 | { |
33 | extension = new ExampleWindowsInstallerBackendExtension(); | 43 | extension = new ExampleWindowsInstallerBackendExtension(); |
34 | } | 44 | } |
@@ -36,7 +46,7 @@ namespace Example.Extension | |||
36 | { | 46 | { |
37 | extension = null; | 47 | extension = null; |
38 | } | 48 | } |
39 | 49 | ||
40 | return extension != null; | 50 | return extension != null; |
41 | } | 51 | } |
42 | } | 52 | } |