@joebigelow / wix / commits / f1956042

Support passing IServiceProvider to IExtensionFactory's

Rob Mensching committed Oct 24, 2018 at 21:04 UTC f195604266cb2b675d31c21df343e1f2ac8bdb0c
2 files changed +35 -7
src/WixToolset.Core/ExtensibilityServices/ExtensionManager.cs
+19 -1
@@ -16,14 +16,32 @@ namespace WixToolset.Core.ExtensibilityServices
16 private List<IExtensionFactory> extensionFactories = new List<IExtensionFactory>();
17 private Dictionary<Type, List<object>> loadedExtensionsByType = new Dictionary<Type, List<object>>();
18
19 + public ExtensionManager(IServiceProvider serviceProvider)
20 + {
21 + this.ServiceProvider = serviceProvider;
22 + }
23 +
24 + private IServiceProvider ServiceProvider { get; }
25 +
26 public void Add(Assembly extensionAssembly)
27 {
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();
30
31 this.extensionFactories.AddRange(factories);
32 }
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 +
45 public void Load(string extensionPath)
46 {
47 Assembly assembly;
src/test/Example.Extension/ExampleExtensionFactory.cs
+16 -6
@@ -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
3 namespace Example.Extension
4 {
@@ -9,16 +9,26 @@ namespace Example.Extension
9 {
10 private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension;
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 +
22 public bool TryCreateExtension(Type extensionType, out object extension)
23 {
24 if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension))
25 {
16 - if (preprocessorExtension == null)
26 + if (this.preprocessorExtension == null)
27 {
18 - preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine();
28 + this.preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine();
29 }
30
21 - extension = preprocessorExtension;
31 + extension = this.preprocessorExtension;
32 }
33 else if (extensionType == typeof(ICompilerExtension))
34 {
@@ -28,7 +38,7 @@ namespace Example.Extension
38 {
39 extension = new ExampleExtensionData();
40 }
31 - else if (extensionType == typeof(IWindowsInstallerBackendExtension))
41 + else if (extensionType == typeof(IWindowsInstallerBackendBinderExtension))
42 {
43 extension = new ExampleWindowsInstallerBackendExtension();
44 }
@@ -36,7 +46,7 @@ namespace Example.Extension
46 {
47 extension = null;
48 }
39 -
49 +
50 return extension != null;
51 }
52 }