Method/property exports utilizing member variables

Jul 6, 2011 at 9:56 PM

My prism application needs to export a method which utilizes a member variable of its encompassing class.  This led me to the following question, which seems to be fundamental to MEF.

Does method/property exports actual create an instance of the class when the corresponding import is satisfied. If not what happens if the method utilizes a private member variable of the class. Basically it is looking like method/property exports must adhere to the rules of a static method/property.  All examples I have ever seen of exported method seem to be completely self contained, so it would seem they should just be declared static.


Jul 8, 2011 at 4:05 PM


We've performed some tests that verified that, when you export a method, the class in which that method is contained must be initialized in order for the method to be exported.

The class can be initialized once, or each time the export is retrieved, depending on the value of the PartCreationPolicy attribute set on the class containing that method (regardless of whether that class is itself exported or not).

I hope you find this helpful.

Guido Leandro Maliandi

Jul 8, 2011 at 8:05 PM
Edited Jul 8, 2011 at 8:07 PM

Thanks a bunch for the help, I believe this is not a commonly known fact (I searched everywhere).  To bring closure to this topic I would like to ask a few details:

1)  Your response indicates the class is instantiated automatically (Assume no class [Export]) when an import occurs on one of its methods.  When this occurs no handle will exist to the class or non-exported methods/properties.  Basically I was trying to confirm an entire class is instantiated yet only the export method can ever be accessed?

2)  So I might help myself/others what debugging technique did you use to figure out when/if class containing the exported method was being instantiated/referenced by MEF?

Thanks Again,

Jul 12, 2011 at 7:59 PM


From what we've found when creating the sample to verify this, the class cannot be accessed, since it has not been exported. However, you could make your method return the property you wish to access, or the instance of the class it belongs (by making it return 'this'). Note that this is probably not the recommended practice, as it would be better to export the property or the whole class in such cases.

As for the way we've used to confirm this, we've created a class, and exported only a method of it. That method simply printed a message to the logger, which indicated that it was called. We've also added a default constructor to that class, and made the constructor print a message to the logger, indicating that the class had been instantiated. Then, we confirmed this by checking the logs, where the message indicating that the class had been constructed appeared prior to the message indicating that the method had been called.

I hope you find this helpful.

Guido Leandro Maliandi