I know there have been lot of questions regarding constructor parameter injection using MEF, but mine is a bit different.
I want to know that is there any way to pass different parameter values to the constructor of a part when I am using the combination of PartCreationPolicy(CreationPolicy.NonShared)
and GetExportedValue
?
For example:
[PartCreationPolicy(CreationPolicy.NonShared)]
[Export]
public partial class Foo
{
[ImportingConstructor]
public Foo([Import("SomeParam")]object parameter)
{
...
}
}
and somewhere else...
container.ComposeExportedValue("SomeParam", "Some value...");
var instance = container.GetExportedValue<Foo>();
In the above example, I can use ComposeExportedValue
only once, as running it a second time will cause a ChangeRejectedException
.
So, my questions are:
SomeParam
in the above scenario, for each new instance?System.Collections.Concurrent.ConcurrentQueue
where I enqueue a parameter value before calling GetExportedValue
and then dequeue the value in the constructor of the part. But that is a hack and also creates more issues than it solves.Thanks for any help. :)
Regards,
Yogesh Jagota
If the answer to both the above questions is no, then are there any other ways to accomplish this with a combination of MEF and some other DI/IOC framework?
I think the answer to question 1 and 2 is indeed no.
I would try AutoFac which gives you more fine grained control and integrates with MEF. For example, it allows you to set up registrations like this so that Bar
and Baz
instances get their Foo
instance with a different parameter:
builder.Register(c => new Bar(new Foo(param));
builder.Register(c => new Baz(new Foo(param2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With