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.
You could export your own
Func
instead:and import it elsewhere:
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 whatExportLifetimeContext
provides.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.