In Unity I would do something like this:
Container.RegisterType<MyClass>("MyNamedInstance", new ContainerControlledLifetimeManager());
Then I could put multiple instances of the same class in.
How can I do this in MEF?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It looks like named exports were added in the latest preview release (MEF 2 Preview 3) as part of MEF’s Convention Model (see the “explicit wiring” section).
However, this is not yet available in .NET 4. Also, the APIs in the preview releases are subject to change.
For now, you could create two exports for the same class with different contracts by adding the name to the export’s contract:
and then you could use the same contract for imports:
Instead of using a
MyClassExporterwith property exports you could also put the export attributes on derived classes, which is perhaps easier to understand but doesn’t work on sealed classes: