This question seems to have been asked several times, but I have not yet found an answer that really works. Very simple, how can I remove something from the MEF container.
Even the code shown here https://mef.codeplex.com/wikipage?title=Parts%20Lifetime in AddPart / RemovePart does not work because it will not compile as it is specified. The code shows this:
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(catalog);
var root = new Root();
container.ComposeParts(root);
batch = new CompositionBatch();
batch.RemovePart(root);
container.Compose(batch);
This will not compile because calling RemovePart requires a ComposablePart, which obviously has no root. Other simple examples show how to create a part and delete a part, but the link to the created part (like ComposablePart) is remembered, so deleting just uses this link. I don’t want to store a link to each part in the container whenever they are created, I just want to remove the part from the container at any arbitrary point in my application, without saving links to it.
Here is what I'm trying to do using the exact same template specified in the documentation above:
public class Program
{
[Import]
private IClass myClass;
public Program()
{
var container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
container.ComposeParts(this);
var partToRemove = container.GetExport<IClass>();
var batch = new CompositionBatch();
batch.RemovePart(partToRemove);
container.Compose(batch);
}
public static void Main(string[] args)
{
var program = new Program();
}
}
But this gives me the following compilation error:
1: "System.Lazy" - 'System.ComponentModel.Composition.Primitives.ComposablePart' C:\Users\irbldr.CORP\Documents\Visual Studio 2012\Projects\ConsoleApplication4\Program.cs
, , , .
- MEF?