From f195604266cb2b675d31c21df343e1f2ac8bdb0c Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Wed, 24 Oct 2018 21:04:19 -0700 Subject: Support passing IServiceProvider to IExtensionFactory's --- .../ExtensibilityServices/ExtensionManager.cs | 20 +++++++++++++++++++- .../Example.Extension/ExampleExtensionFactory.cs | 22 ++++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) (limited to 'src') 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 private List extensionFactories = new List(); private Dictionary> loadedExtensionsByType = new Dictionary>(); + public ExtensionManager(IServiceProvider serviceProvider) + { + this.ServiceProvider = serviceProvider; + } + + private IServiceProvider ServiceProvider { get; } + public void Add(Assembly extensionAssembly) { var types = extensionAssembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && typeof(IExtensionFactory).IsAssignableFrom(t)); - var factories = types.Select(t => (IExtensionFactory)Activator.CreateInstance(t)).ToList(); + var factories = types.Select(this.CreateExtensionFactory).ToList(); this.extensionFactories.AddRange(factories); } + private IExtensionFactory CreateExtensionFactory(Type type) + { + var constructor = type.GetConstructor(new[] { typeof(IServiceProvider) }); + if (constructor != null) + { + return (IExtensionFactory)constructor.Invoke(new[] { this.ServiceProvider }); + } + + return (IExtensionFactory)Activator.CreateInstance(type); + } + public void Load(string extensionPath) { 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 @@ -// 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. +// 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. namespace Example.Extension { @@ -9,16 +9,26 @@ namespace Example.Extension { private ExamplePreprocessorExtensionAndCommandLine preprocessorExtension; + public ExampleExtensionFactory(IServiceProvider serviceProvider) + { + this.ServiceProvider = serviceProvider; + } + + /// + /// This exists just to show it is possible to get a service provider to the extension factory. + /// + private IServiceProvider ServiceProvider { get; } + public bool TryCreateExtension(Type extensionType, out object extension) { if (extensionType == typeof(IExtensionCommandLine) || extensionType == typeof(IPreprocessorExtension)) { - if (preprocessorExtension == null) + if (this.preprocessorExtension == null) { - preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); + this.preprocessorExtension = new ExamplePreprocessorExtensionAndCommandLine(); } - extension = preprocessorExtension; + extension = this.preprocessorExtension; } else if (extensionType == typeof(ICompilerExtension)) { @@ -28,7 +38,7 @@ namespace Example.Extension { extension = new ExampleExtensionData(); } - else if (extensionType == typeof(IWindowsInstallerBackendExtension)) + else if (extensionType == typeof(IWindowsInstallerBackendBinderExtension)) { extension = new ExampleWindowsInstallerBackendExtension(); } @@ -36,7 +46,7 @@ namespace Example.Extension { extension = null; } - + return extension != null; } } -- cgit v1.2.3-55-g6feb