Skip to content

Commit

Permalink
fixing PreserveAttribute to ensure container is loaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Dec 26, 2019
1 parent d3a4385 commit 181a763
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Prism.Container.Extensions/ContainerLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Assembly> assemblies, out ContainerExtensionAttribute attribute)
{
attribute = null;
Expand Down
5 changes: 5 additions & 0 deletions src/Prism.Container.Extensions/PreserveAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using Prism.Ioc;

namespace Prism.Container.Extensions
{
Expand Down Expand Up @@ -36,6 +37,10 @@ public PreserveAttribute()
public PreserveAttribute(Type referenceType)
: this(true, false)
{
if(typeof(IContainerExtension).IsAssignableFrom(referenceType))
{
ContainerLocator.LocatePreservedReference(referenceType);
}
}
}
}

0 comments on commit 181a763

Please sign in to comment.