gpt4 book ai didi

c# - 如何让 DirectoryModuleCatalog 工作?

转载 作者:行者123 更新时间:2023-11-30 15:35:53 26 4
gpt4 key购买 nike

我正在为一个 WPF 项目使用 Prism 4 和 MEF。我有一些需要从目录加载的 DLL。这些 DLL 通过 IGame 实现了 IModule 并且格式正确(或者至少我认为是这样):

[Module(ModuleName = "SnakeModule")]
class SnakeModule : IGame
{
public void Initialize()
{
Console.WriteLine("test");
}

public void StartGame()
{
throw new NotImplementedException();
}

}

目前,主项目正在编译中,但模块尚未初始化。我无法理解如何设置我的 Bootstrap ,而且文档也没有太大帮助,因为它没有包含 DirectoryModuleCatalog 的完整示例。模块化快速入门也没有编译。这是我的 Bootstrap :

class BootStrap : MefBootstrapper
{

protected override DependencyObject CreateShell()
{
return ServiceLocator.Current.GetInstance<Shell>();
}

protected override void InitializeShell()
{
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}

protected override void ConfigureAggregateCatalog()
{
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrap).Assembly));
}


protected override IModuleCatalog CreateModuleCatalog()
{

DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = @"..\..\..\GameTestLib\bin\Debug" };
return catalog;
}

protected override void ConfigureContainer()
{
base.ConfigureContainer();
}

}

DLL 的路径是正确的。总而言之,我的问题是:我应该如何设置我的 Bootstrap ?

最佳答案

首先,由于您使用的是 Prism,我建议您使用 ModuleExport,如下所示:

[ModuleExport("SnakeModule", typeof(IGame))]

但您的问题实际上来自于您没有将您的类设置为公共(public)类,因此阻止了您的模块的发现。因此,您需要将代码更改为:

[ModuleExport("SnakeModule", typeof(IGame))]
public class SnakeModule : IGame
{
public void Initialize()
{
Console.WriteLine("test");
}

public void StartGame()
{
throw new NotImplementedException();
}

}

应该没问题的!

关于c# - 如何让 DirectoryModuleCatalog 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com