Skip to main content
GameDev.net gamedev.net
🔒 Locked

MEF and releasing imports.

Started by Earthmark Jul 26, 2013 at 1:55 AM 1 replies 2.1k views
Original Post
Earthmark
Earthmark

I am attempting to use MEF to load content in a program, the point is that the program is extendable so that additional modules can be easily added. However I am having trouble with MEF returning the same object repeatedly, I am not using Import attributes and am instead using the manual import methods.

I am using a custom export attribute for the classes to be composed.


[AttributeUsage(AttributeTargets.Class), PartCreationPolicy(CreationPolicy.NonShared)]
public sealed class ItemExportAttribute : ExportAttribute
{
	public ItemExportAttribute(string name)
		: base(name, typeof(Item)) {}
}

Even though I am using NonShared the same export keeps being provided, I am also composing subparts that change between calling GetExportedValue, so every object should be different. However even stepping through the debugger the constructor is only called once. Also without the NonShared policy an exception is thrown when it attempts to import an object with the same contract twice.

As far as I can tell this is mostly caused by not calling the ReleaseExport method on the CompositonContainer, however that seems rather intensive and fairly roundabout to the point where I get a feeling I'm wrong on what these parts do.

Edit: Turns out that does not fix the problem, it still only outputs one object.

Does anyone know a way to get imports without requiring every export to then be released again directly after it is imported?

Unnamed User
Unnamed User
It's been a while since I worked with MEF but I think the problem is that you put the PartCreationPolicy in your custom attribute, it should be directly in the exported class, base class or interface.
Earthmark
Earthmark

That seems to have done it. Looks as though having that attribute is now mandatory, oh well.

Thank you Leandro.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.