初始化惰性实例时将参数传递给构造函数

发布于 2024-10-06 15:16:05 字数 216 浏览 0 评论 0原文

据我所知,如果一个变量被声明为 Lazy,那么当我们使用 Value 属性时就会调用它的构造函数。

我需要将一些参数传递给这个 Lazy 实例,但找不到正确的语法。 这不是我的设计,我正在使用 MEF 和 ExportFactory,它返回我的部件的 Lazy 实例。我的部件有构造函数,我需要使用一些参数调用这些构造函数。

As I know if a variable is declared Lazy, then its constructor is called when we use the Value property.

I need to pass some parameters to this Lazy instance but cannot find the correct syntax.
This is not my design, I'm using MEF and ExportFactory, it returns me Lazy instances of my parts. My parts have constructors and I need to call these constructors with some parameters.

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

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

发布评论

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

评论(2

稚气少女 2024-10-13 15:16:05

您可以导出自己的 Func 相反:

public class FooFactory
{
    [Export(typeof(Func<string,int,ExportLifetimeContext<IFoo>>))]
    public ExportLifetimeContext<IFoo> CreateFoo(string param1, int param2)
    {
        Foo foo = new Foo(param1, param2);
        return new ExportLifetimeContext<IFoo>(foo,
            delegate
            {
                // Clean-up action code goes here. The client might not be able 
                // to do this through the IFoo interface because it might not
                // even expose a Dispose method.
                //
                // If you created other hidden dependencies in order to construct
                // Foo, you could also clean them up here. 
                foo.Dispose();
            });
    }
}

并将其导入到其他地方:

[Export(typeof(ISomething))]
public class FooUser : ISomething
{
    private readonly Func<string,int,ExportLifetimeContext<IFoo>> fooFactory;

    [ImportingConstructor]
    public FooUser(Func<string,int,ExportLifetimeContext<IFoo>> fooFactory)
    {
        this.fooFactory = fooFactory;
    }

    public void DoSomething()
    {
        using (var fooLifetime = this.fooFactory("hello", 3))
        {
            IFoo foo = fooLifetime.Value;
            ...
        }
    }
}

如果您不需要清理操作,那么您可以通过扔掉所有 ExportLifetimeContext 来大大简化这一过程东西。

但是,IFoo 的某些实现可能是一次性的(或依赖于其他一次性对象),而其他实现则不是。因此,最正确的做法是在抽象中构建一个“我已完成此对象”信号,这就是 ExportLifetimeContext 提供的。

You could export your own Func instead:

public class FooFactory
{
    [Export(typeof(Func<string,int,ExportLifetimeContext<IFoo>>))]
    public ExportLifetimeContext<IFoo> CreateFoo(string param1, int param2)
    {
        Foo foo = new Foo(param1, param2);
        return new ExportLifetimeContext<IFoo>(foo,
            delegate
            {
                // Clean-up action code goes here. The client might not be able 
                // to do this through the IFoo interface because it might not
                // even expose a Dispose method.
                //
                // If you created other hidden dependencies in order to construct
                // Foo, you could also clean them up here. 
                foo.Dispose();
            });
    }
}

and import it elsewhere:

[Export(typeof(ISomething))]
public class FooUser : ISomething
{
    private readonly Func<string,int,ExportLifetimeContext<IFoo>> fooFactory;

    [ImportingConstructor]
    public FooUser(Func<string,int,ExportLifetimeContext<IFoo>> fooFactory)
    {
        this.fooFactory = fooFactory;
    }

    public void DoSomething()
    {
        using (var fooLifetime = this.fooFactory("hello", 3))
        {
            IFoo foo = fooLifetime.Value;
            ...
        }
    }
}

If you don't need the clean-up action then you could simplify this considerably by throwing out all the ExportLifetimeContext stuff.

However, some implementations of IFoo might be disposable (or depend on other disposable objects) while others are not. So the most correct thing to do is to build a "I'm done with this object" signal into the abstraction, which is what ExportLifetimeContext provides.

旧竹 2024-10-13 15:16:05

当您使用 ExportFactory 创建零件时,MEF 没有内置方法可将构造函数参数传递给零件。像 Wim Coenen 所建议的那样可能是实现你想要的目标的最佳方法。

MEF doesn't have a built-in way for you to pass constructor parameters to a part when you create it with an ExportFactory. Something like what Wim Coenen suggests is probably the best way to achieve what you want.

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