Jump to content
  • Advertisement
Sign in to follow this  
Earthmark

MEF and releasing imports.

This topic is 1815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by Earthmark

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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

 

Thank you Leandro.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!