运行目录的所有模块(棱镜)

发布于 2024-09-24 19:03:56 字数 1152 浏览 0 评论 0原文

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

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

我确实喜欢这样:

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()

i'm using the desktop library of prism.

what i want is to get modules in a directory and then, run them.

I do like that:

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

I checked, the modules are loaded in the catalog.
Example of a module:

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();
        }
    }

But then i want to run them (launch their Initialize()) but i don't find it. I want to run the whole catalog. someone has an idea ? I tried catalog.Initialize(), catalog.Validate() or catalog.Load()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Hello爱情风 2024-10-01 19:03:56

您的代码看起来正确,我的印象是您必须重写 Bootstrapper 类中的 GetModuleCatalog() 方法才能执行此操作。这是一个非常简单的 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();

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

Your code looks correct, I was under the impression that you had to override the GetModuleCatalog() method in your Bootstrapper class in order to do this. Here is a working example of a pretty straight forward Bootstrapper that loads modules from a modules directory.

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

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

Update

It is probably possible to not use a bootstrapper and load your modules, but I do not see why you would not take advantage of the UnityBootstrapper class, it does the work for you.

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

Your modules will be loaded. I myself have never done this without using the bootstrapper because it is very straightforward.

箹锭⒈辈孓 2024-10-01 19:03:56

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

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

正如我之前所说,上面的操作是由 bootstrapper 为您完成的。
我希望这有帮助。

谢谢,
达米安

As jsmith said, the default bootstrapper provides the heavy lifting for the configuration that you would otherwise perform yourself. If you don't have a Shell, simply create a custom bootstrapper that skips over that part.

In case you don't want to use a bootstrapper by any means, you can just instantiate the ModuleManager class, passing the ModulesCatalog as one of the parameters and call the ModuleManager's Run method.

As I said before, the above is done by the bootstrapper for you.
I hope this helps.

Thanks,
Damian

飘逸的'云 2024-10-01 19:03:56

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

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

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 引导程序,您需要的只是 Unity 平台,该平台可与 < a href="http://compositewpf.codeplex.com/" rel="nofollow">棱镜。

How are you bootstrapping your application? Have you created a bootstrapper class? It is the bootstrapper that initializes all of your modules.

This is an example of a bootstrapper class I made.

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;
    }

}

Then in your App.xaml file's OnStartup, you run your bootstrapper and it will call initialize on all of your modules.

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

This example uses the Unity bootstrapper, all you need is the Unity platform which is available side-by-side with Prism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文