@ -22,6 +22,7 @@ using System.Diagnostics;
using System.IO ;
using System.IO ;
using System.Linq ;
using System.Linq ;
using System.Reflection ;
using System.Reflection ;
using System.Runtime.Loader ;
using System.Text ;
using System.Text ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows ;
@ -35,6 +36,7 @@ using Microsoft.VisualStudio.Composition;
using TomsToolbox.Wpf.Styles ;
using TomsToolbox.Wpf.Styles ;
namespace ICSharpCode.ILSpy
namespace ICSharpCode.ILSpy
{
{
/// <summary>
/// <summary>
@ -85,8 +87,27 @@ namespace ICSharpCode.ILSpy
ILSpyTraceListener . Install ( ) ;
ILSpyTraceListener . Install ( ) ;
}
}
static Assembly ResolvePluginDependencies ( AssemblyLoadContext context , AssemblyName assemblyName )
{
#if !NET472
var rootPath = Path . GetDirectoryName ( typeof ( App ) . Assembly . Location ) ;
var assemblyFileName = Path . Combine ( rootPath , assemblyName . Name + ".dll" ) ;
if ( ! File . Exists ( assemblyFileName ) )
return null ;
return context . LoadFromAssemblyPath ( assemblyFileName ) ;
#else
throw new NotImplementedException ( ) ;
#endif
}
private static async Task InitializeMef ( )
private static async Task InitializeMef ( )
{
{
#if !NET472
// Add custom logic for resolution of dependencies.
// This necessary because the AssemblyLoadContext.LoadFromAssemblyPath and related methods,
// do not automatically load dependencies.
AssemblyLoadContext . Default . Resolving + = ResolvePluginDependencies ;
#endif
// Cannot show MessageBox here, because WPF would crash with a XamlParseException
// Cannot show MessageBox here, because WPF would crash with a XamlParseException
// Remember and show exceptions in text output, once MainWindow is properly initialized
// Remember and show exceptions in text output, once MainWindow is properly initialized
try
try
@ -98,7 +119,6 @@ namespace ICSharpCode.ILSpy
var discovery = new AttributedPartDiscoveryV1 ( Resolver . DefaultInstance ) ;
var discovery = new AttributedPartDiscoveryV1 ( Resolver . DefaultInstance ) ;
var catalog = ComposableCatalog . Create ( Resolver . DefaultInstance ) ;
var catalog = ComposableCatalog . Create ( Resolver . DefaultInstance ) ;
var pluginDir = Path . GetDirectoryName ( typeof ( App ) . Module . FullyQualifiedName ) ;
var pluginDir = Path . GetDirectoryName ( typeof ( App ) . Module . FullyQualifiedName ) ;
#if NET472
if ( pluginDir ! = null )
if ( pluginDir ! = null )
{
{
foreach ( var plugin in Directory . GetFiles ( pluginDir , "*.Plugin.dll" ) )
foreach ( var plugin in Directory . GetFiles ( pluginDir , "*.Plugin.dll" ) )
@ -106,7 +126,11 @@ namespace ICSharpCode.ILSpy
var name = Path . GetFileNameWithoutExtension ( plugin ) ;
var name = Path . GetFileNameWithoutExtension ( plugin ) ;
try
try
{
{
#if NET472
var asm = Assembly . Load ( name ) ;
var asm = Assembly . Load ( name ) ;
#else
var asm = AssemblyLoadContext . Default . LoadFromAssemblyPath ( plugin ) ;
#endif
var parts = await discovery . CreatePartsAsync ( asm ) ;
var parts = await discovery . CreatePartsAsync ( asm ) ;
catalog = catalog . AddParts ( parts ) ;
catalog = catalog . AddParts ( parts ) ;
}
}
@ -116,7 +140,6 @@ namespace ICSharpCode.ILSpy
}
}
}
}
}
}
#endif
// Add the built-in parts
// Add the built-in parts
var createdParts = await discovery . CreatePartsAsync ( Assembly . GetExecutingAssembly ( ) ) ;
var createdParts = await discovery . CreatePartsAsync ( Assembly . GetExecutingAssembly ( ) ) ;
catalog = catalog . AddParts ( createdParts ) ;
catalog = catalog . AddParts ( createdParts ) ;