2

我正在使用棱镜的桌面库。

我想要的是在目录中获取模块,然后运行它们。

我喜欢这样:

DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";

我检查了,模块已加载到目录中。模块示例:

public class SendEmailClass : IModule
    {
        public void SendEmail()
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("**", "moi");
            mail.Subject = "Report"; //manage generated subject

            mail.To.Add("***");

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
            smtp.Port = 57;
            smtp.EnableSsl = true; //depending of the smtp server
            NetworkCredential cred = new NetworkCredential("***", "***");
            smtp.Credentials = cred;
            smtp.Send(mail);
        }

        public void Initialize()
        {
            SendEmail();
        }
    }

但后来我想运行它们(启动它们的 Initialize()),但我没有找到它。我想运行整个目录。有人有想法吗?我尝试了 catalog.Initialize()、catalog.Validate() 或 catalog.Load()

4

3 回答 3

2

你的代码看起来是正确的,我的印象是你必须重写GetModuleCatalog()你的Bootstrapper类中的方法才能做到这一点。Bootstrapper这是一个从模块目录加载模块的非常简单的工作示例。

public class Bootstrapper : UnityBootstrapper
{
    private const string MODULE_FOLDER = @".\modules";

    protected override IModuleCatalog GetModuleCatalog()
    {
        DirectoryModuleCatalog catalog = new DirectoryModuleCatalog() { ModulePath = MODULE_FOLDER };
        return catalog;
    }
}

更新

可能不使用引导程序并加载您的模块,但我不明白您为什么不利用UnityBootstrapper该类,它为您工作。

Bootstrapper bootStrapper = new Bootstrapper();
bootStrapper.Run();

您的模块将被加载。我自己从来没有在不使用引导程序的情况下这样做,因为它非常简单。

于 2010-09-27T15:50:11.097 回答
1

正如 jsmith 所说,默认引导程序为您自己执行的配置提供了繁重的工作。如果您没有 Shell,只需创建一个跳过该部分的自定义引导程序。

如果您不想以任何方式使用引导程序,您可以只实例化ModuleManager类,将 ModulesCatalog 作为参数之一传递并调用 ModuleManager 的Run方法。

正如我之前所说,以上是由引导程序为您完成的。我希望这有帮助。

谢谢,达米安

于 2010-09-27T16:32:56.860 回答
0

你是如何引导你的应用程序的?您是否创建了引导程序类?它是初始化所有模块的引导程序。

这是我制作的引导程序类的示例。

public class ApplicationBootstrapper : UnityBootstrapper
{
    // Here is where you create your module catalog
    protected override IModuleCatalog GetModuleCatalog()
    {
        DirectoryModuleCatalog catalog = new DirectoryModuleCatalog();
        catalog.ModulePath = @"C:\Users\Raph\Documents\Visual Studio 2010\Projects\LibraryLoad\LibraryLoad\Modules";

        return catalog;
    }

    // Here is where you create your user interface shell.
    protected override DependencyObject CreateShell()
    {
        Container.RegisterInstance<IApplicationSettings>(new ApplicationSettings());

        Shell shell = Container.Resolve<Shell>();

        if (shell != null)
            shell.Show();

        return shell;
    }

}

然后在你的App.xaml文件中OnStartup,你运行你的引导程序,它会在你的所有模块上调用初始化。

protected override void OnStartup(StartupEventArgs e)
{
    ApplicationBootstrapper bootstrapper = new ApplicationBootstrapper();
    bootstrapper.Run();
}

此示例使用 Unity 引导程序,您所需要的只是与Prism并行可用的Unity 平台

于 2011-01-13T19:11:58.563 回答