MEF Usage Notes
2022-02-17 13:21:18

模块代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[Export(typeof(Window))]
[ExportMetadata("name", "首页")]
public partial class MainView : Window
{
public MainView()
{
InitializeComponent();
}
}

# .NET Framework 版本
public class WindowsCompos
{
[Export("extWindows")]
[ImportMany(typeof(Window))]
public IEnumerable<ExportFactory<Window, IDictionary<string, object>>> ExtWindows { get; set; }
}

# .NET 版本(需要先安装 System.Composition)
public class WindowsCompos
{
[Export("extWindows")]
[ImportMany]
public IEnumerable<ExportFactory<Window, IDictionary<string, object>>> ExtWindows { get; set; }
}

主程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# .NET Framework 版本
var extAss = Assembly.Load("MultiToolKit.WpfApp.Modules.Home");
var catalog = new AssemblyCatalog(extAss);
var container = new CompositionContainer(catalog);

var extWindowsList =
container.GetExportedValue<IEnumerable<ExportFactory<Window, IDictionary<string, object>>>>(
"extWindows");

# .NET 版本
var executableLocation = Assembly.GetEntryAssembly().Location;
var path = Path.GetDirectoryName(executableLocation);
var assemblies = Directory
.GetFiles(path, "*.dll", SearchOption.AllDirectories)
.Select(AssemblyLoadContext.Default.LoadFromAssemblyPath)
.ToList();
var configuration = new ContainerConfiguration()
.WithAssemblies(assemblies);
using var container = configuration.CreateContainer();
_extWindowsList = container.GetExports<ExportFactory<Window, IDictionary<string, object>>>();
Prev
2022-02-17 13:21:18
Next