使用Ninject和wpf从所有程序集中加载模块

本文关键字:程序集 程序 集中 加载 模块 Ninject wpf 使用 | 更新日期: 2023-09-27 18:25:16

我使用了这篇文章,并在项目中创建了多个类库。我想在内核中加载所有模块。

为了加载所有模块,我在MainViewModel 中使用此代码

public MainViewModel()
{
    IKernel kernel = new StandardKernel();
    kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
    Plugins = kernel.GetAll<PluginBase>().ToList();
}

但不要在AppDomain.CurrentDomain.GetAssemblies() 中加载模块(插件)

使用Ninject和wpf从所有程序集中加载模块

我将此代码用于加载程序集。我在那里找到的。

var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));