棱镜+ MEF:如何将参数正确加载到我的一项服务中?

发布于 2024-10-20 15:40:50 字数 1508 浏览 5 评论 0原文

基本上我有以下场景:

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    string x = (e.Args.Length > 0) ? e.Args[0];
    string y = (e.Args.Length > 1) ? e.Args[1];

    Bootstrapper bootstrapper = new MyBootstrapper(x, y);
    bootstrapper.Run();
}

MyBootstrapper.cs:

public sealed class MyBootstrapper : MefBootstrapper
{
    private string _x;
    private string _y;

    public MyBootstrapper(string x, string y)
    {
        _x = x;
        _y = y;
    }

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();

        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
    }

    protected override DependencyObject CreateShell()
    {
        return Container.GetExportedValue<ClientShell>();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        Application.Current.MainWindow = (Window)Shell;
        Application.Current.MainWindow.Show();
    }
}

FooBarService.cs

public interface IFooBarService
{
    string x { get; }
    string y { get; }
}

[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
    string x { get; protected set; }
    string y { get; protected set; }
}

如何将 x 和 y 加载到我的服务正常吗?另外,如何确保执行此操作时它不会与我的容器等中的其他任何内容发生冲突?

Basically I have the following scenario:

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    string x = (e.Args.Length > 0) ? e.Args[0];
    string y = (e.Args.Length > 1) ? e.Args[1];

    Bootstrapper bootstrapper = new MyBootstrapper(x, y);
    bootstrapper.Run();
}

MyBootstrapper.cs:

public sealed class MyBootstrapper : MefBootstrapper
{
    private string _x;
    private string _y;

    public MyBootstrapper(string x, string y)
    {
        _x = x;
        _y = y;
    }

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();

        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
    }

    protected override DependencyObject CreateShell()
    {
        return Container.GetExportedValue<ClientShell>();
    }

    protected override void InitializeShell()
    {
        base.InitializeShell();

        Application.Current.MainWindow = (Window)Shell;
        Application.Current.MainWindow.Show();
    }
}

FooBarService.cs

public interface IFooBarService
{
    string x { get; }
    string y { get; }
}

[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
    string x { get; protected set; }
    string y { get; protected set; }
}

How do I load x and y into my service properly? Also, how do I ensure that when doing this it doesn't collide with anything else in my Container or the such?

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

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

发布评论

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

评论(1

染火枫林 2024-10-27 15:40:50

您不必使用 StartupEventArgs。您的 FooBarService 可以简单地使用 Environment.GetCommandLineArgs ,如下所示:

[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
    public void FooBarService()
    {
        var args = Environment.GetCommandLineArgs();
        x = (args.Length > 0) ? args[0]:"";
        y = (args.Length > 1) ? args[1]:"";
    }
    string x { get; protected set; }
    string y { get; protected set; }
}

编辑:我不确定 args[0] 是第一个参数还是程序调用,您必须尝试一下,在这种情况下进一步切换索引!

You don't have to use the StartupEventArgs. Your FooBarService could simply use Environment.GetCommandLineArgs like this:

[Export("FooBarService", typeof(IFooBarService))]
public class FooBarService : IFooBarService
{
    public void FooBarService()
    {
        var args = Environment.GetCommandLineArgs();
        x = (args.Length > 0) ? args[0]:"";
        y = (args.Length > 1) ? args[1]:"";
    }
    string x { get; protected set; }
    string y { get; protected set; }
}

EDIT: I'm not sure if args[0] is the first parameter or the program call, you have to try that out and in that case switch indexes one further!

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