-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ef542e
commit 12a8f32
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
title: MEF Notes | ||
date: 2022-02-17 21:21:18 | ||
tags: WPF | ||
--- | ||
|
||
# 模块代码 | ||
|
||
```C# | ||
[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; } | ||
} | ||
``` | ||
|
||
# 主程序代码 | ||
|
||
```C# | ||
# .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>>>(); | ||
``` |