Getting 2x parts when using strongly typed metadata

Nov 11, 2009 at 9:09 PM
Edited Nov 11, 2009 at 9:09 PM

I am using MEF in Visual Studio 2010 Beta 2. I have an issue with strongly typed metadata: I get two parts for each import.

This is the attribute:


[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class PluginAttribute : ExportAttribute
public PluginAttribute()
public string Name { get; set; }
public string Description { get; set; }
public string Version { get; set; }


This is the matching interface:


public interface IPluginCapabilities
string Name { get; }
string Description { get; }
string Version { get; }

This is my interface my exports implement


public interface IMyPluginInterface
// methods come here


Here is a part:

Name = "Plugin 1",
Description = "First plugin",
Version = "")]
public class Plugin1 : IMyPluginInterface

To use this, I have:


Lazy<IMyPluginInterface, IPluginCapabilities>[] m_plugins;

though it also exhibits the same behavior with



Lazy<IMyPluginInterface, IDictionary<string, object>>[] m_plugins;


However, when not using the PluginAttribute, but ExportMetadata (of course with the later version of the array of Lazy objects), then I get only one part for each export.


[ExportMetadata("Name", "Plugin 1")]
[ExportMetadata("Description", "First plugin")]
[ExportMetadata("Version", "")]
public class Plugin1 : IMyPluginInterface


What is the problem? Is there some initialization or maybe filtering I have to apply in this case? There is nothing that I found on the documentation, which I just followed.

Thank you.

Nov 11, 2009 at 11:43 PM

The reason you are actually getting two Exports is because you are exporting it twice. Once with the Export attribute and once with the PluginAttribute which is a sub-type of ExportAttribute. I would simply remove the explicit Export attribute from your plugin type and you should be good to go.

Nov 12, 2009 at 3:15 PM

This was it. It should have been pretty simple to figure it out: the PluginAttribute was derived from the ExportAttribute, so of course I was exporting it twice.

Thanks for your answer.