Skip to content

Commit

Permalink
working on .NET Core 3 module catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed May 24, 2019
1 parent ff9641a commit 805eca4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
65 changes: 65 additions & 0 deletions Source/Wpf/Prism.Wpf/Modularity/DirectoryModuleCatalog.Core.cs
@@ -0,0 +1,65 @@
#if NETCOREAPP3_0

using Prism.Properties;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Prism.Modularity
{
public class DirectoryModuleCatalog : ModuleCatalog
{
/// <summary>
/// Directory containing modules to search for.
/// </summary>
public string ModulePath { get; set; }

/// <summary>
/// Drives the main logic of building the child domain and searching for the assemblies.
/// </summary>
protected override void InnerLoad()
{
if (string.IsNullOrEmpty(this.ModulePath))
throw new InvalidOperationException(Resources.ModulePathCannotBeNullOrEmpty);

if (!Directory.Exists(this.ModulePath))
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, Resources.DirectoryNotFound, this.ModulePath));

AppDomain appDomain = AppDomain.CurrentDomain;

try
{
List<string> loadedAssemblies = new List<string>();

var assemblies = from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()
where !(assembly is System.Reflection.Emit.AssemblyBuilder)
&& assembly.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder"
&& !String.IsNullOrEmpty(assembly.Location)
select assembly.Location;

loadedAssemblies.AddRange(assemblies);

//Type loaderType = typeof(InnerModuleInfoLoader);

//if (loaderType.Assembly != null)
//{
// var loader = (InnerModuleInfoLoader)appDomain.CreateInstanceFrom(loaderType.Assembly.Location, loaderType.FullName).Unwrap();
// loader.LoadAssemblies(loadedAssemblies);
// this.Items.AddRange(loader.GetModuleInfos(this.ModulePath));
//}
}
catch
{
//do nothing
}
}
}
}
#endif
35 changes: 15 additions & 20 deletions Source/Wpf/Prism.Wpf/Modularity/DirectoryModuleCatalog.Desktop.cs
@@ -1,4 +1,4 @@

#if NET45

using Prism.Properties;
using System;
Expand Down Expand Up @@ -41,11 +41,8 @@ protected override void InnerLoad()
if (!Directory.Exists(this.ModulePath))
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, Resources.DirectoryNotFound, this.ModulePath));
#if NETCOREAPP3_0
AppDomain childDomain = AppDomain.CurrentDomain;
#else

AppDomain childDomain = this.BuildChildDomain(AppDomain.CurrentDomain);
#endif

try
{
Expand All @@ -54,7 +51,7 @@ protected override void InnerLoad()
var assemblies = (
from Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()
where !(assembly is System.Reflection.Emit.AssemblyBuilder)
&& assembly.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder"
&& assembly.GetType().FullName != "System.Reflection.Emit.InternalAssemblyBuilder"
&& !String.IsNullOrEmpty(assembly.Location)
select assembly.Location
);
Expand All @@ -74,13 +71,11 @@ select assembly.Location
}
finally
{
#if NET45
AppDomain.Unload(childDomain);
#endif
}
}

#if NET45

/// <summary>
/// Creates a new child domain and copies the evidence from a parent domain.
/// </summary>
Expand All @@ -105,7 +100,6 @@ protected virtual AppDomain BuildChildDomain(AppDomain parentDomain)
AppDomainSetup setup = parentDomain.SetupInformation;
return AppDomain.CreateDomain("DiscoveryRegion", evidence, setup);
}
#endif

private class InnerModuleInfoLoader : MarshalByRefObject
{
Expand All @@ -115,7 +109,7 @@ internal ModuleInfo[] GetModuleInfos(string path)
DirectoryInfo directory = new DirectoryInfo(path);

ResolveEventHandler resolveEventHandler =
delegate(object sender, ResolveEventArgs args) { return OnReflectionOnlyResolve(args, directory); };
delegate (object sender, ResolveEventArgs args) { return OnReflectionOnlyResolve(args, directory); };

AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += resolveEventHandler;

Expand Down Expand Up @@ -214,15 +208,15 @@ private static ModuleInfo CreateModuleInfo(Type type)
switch (argumentName)
{
case "ModuleName":
moduleName = (string) argument.TypedValue.Value;
moduleName = (string)argument.TypedValue.Value;
break;

case "OnDemand":
onDemand = (bool) argument.TypedValue.Value;
onDemand = (bool)argument.TypedValue.Value;
break;

case "StartupLoaded":
onDemand = !((bool) argument.TypedValue.Value);
onDemand = !((bool)argument.TypedValue.Value);
break;
}
}
Expand All @@ -234,20 +228,21 @@ private static ModuleInfo CreateModuleInfo(Type type)

foreach (CustomAttributeData cad in moduleDependencyAttributes)
{
dependsOn.Add((string) cad.ConstructorArguments[0].Value);
dependsOn.Add((string)cad.ConstructorArguments[0].Value);
}

ModuleInfo moduleInfo = new ModuleInfo(moduleName, type.AssemblyQualifiedName)
{
InitializationMode =
{
InitializationMode =
onDemand
? InitializationMode.OnDemand
: InitializationMode.WhenAvailable,
Ref = type.Assembly.EscapedCodeBase,
};
Ref = type.Assembly.EscapedCodeBase,
};
moduleInfo.DependsOn.AddRange(dependsOn);
return moduleInfo;
}
}
}
}
}
#endif

0 comments on commit 805eca4

Please sign in to comment.