From 181a763860cf416152cf1fb658e96a93aa8df9a0 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Thu, 26 Dec 2019 11:32:37 -0800 Subject: [PATCH] fixing PreserveAttribute to ensure container is loaded. --- src/Prism.Container.Extensions/ContainerLocator.cs | 13 +++++++++++++ src/Prism.Container.Extensions/PreserveAttribute.cs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/Prism.Container.Extensions/ContainerLocator.cs b/src/Prism.Container.Extensions/ContainerLocator.cs index dc81a55..0f3f855 100644 --- a/src/Prism.Container.Extensions/ContainerLocator.cs +++ b/src/Prism.Container.Extensions/ContainerLocator.cs @@ -29,6 +29,19 @@ public static IContainerExtension Locate() return _locatedExtension.Container; } + internal static void LocatePreservedReference(Type type) + { + if (_locatedExtension is null) + { + if (!LocatedAssembly(new[] { type.Assembly }, out _locatedExtension)) + { + throw new DllNotFoundException("No assembly containing a reference to an IContainerExtension implementation could be found. You must have a reference to one of the DI Container Extensions in your final project"); + } + + Console.WriteLine($"Located Container: {_locatedExtension.Container.GetType().FullName}"); + } + } + private static bool LocatedAssembly(IEnumerable assemblies, out ContainerExtensionAttribute attribute) { attribute = null; diff --git a/src/Prism.Container.Extensions/PreserveAttribute.cs b/src/Prism.Container.Extensions/PreserveAttribute.cs index ef26b56..07264f7 100644 --- a/src/Prism.Container.Extensions/PreserveAttribute.cs +++ b/src/Prism.Container.Extensions/PreserveAttribute.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using Prism.Ioc; namespace Prism.Container.Extensions { @@ -36,6 +37,10 @@ public PreserveAttribute() public PreserveAttribute(Type referenceType) : this(true, false) { + if(typeof(IContainerExtension).IsAssignableFrom(referenceType)) + { + ContainerLocator.LocatePreservedReference(referenceType); + } } } }