Based on the answer for this question What's the difference between CompositionBatch and catalogs? which states that the use of composition batch is useful in situations where objects are built by some other component. This made me think, what real life example does it apply to? Then I started thinking about objects that get built that you aren't in control of. How would you add those instances to the container if you can't add parts that aren't marked as an Export? The only thing that comes to mind is to setup a class with a member of the desired type and marking it as an export.
Is it possible to add instances to a container if they aren't marked as an export?
I'm going to assume that you meant "classes not marked as an export" instead of "instances".
For .NET4.5 and up you can use MEF's convention model.
For .NET4, Mark Seemann has blogged about how you can make clever use of generics and property exports to export existing types. You can create a class like this:
public class MefAdapter<T> where T : new()
{
private readonly T export;
public MefAdapter()
{
this.export = new T();
}
[Export]
public virtual T Export
{
get { return this.export; }
}
}
And then you can construct a type catalog to export existing types which are not MEF-aware like this:
var catalog = new TypeCatalog(
typeof(MefAdapter<Foo>),
typeof(MefAdapter<Bar>),
...);
You could modify this example to add support for types with constructor arguments, exporting interfaces instead of just the class type, etcetera.