Getting 2x parts when using strongly typed metadata

Nov 11, 2009 at 10:09 PM
Edited Nov 11, 2009 at 10: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:

 

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class PluginAttribute : ExportAttribute
{
public PluginAttribute()
:base(typeof(IMyPluginInterface))
{
}
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:

[Export(typeof(IMyPluginInterface))]
[Plugin(
Name = "Plugin 1",
Description = "First plugin",
Version = "1.0.0.0")]
public class Plugin1 : IMyPluginInterface
{
}

To use this, I have:

 

[ImportMany(typeof(IMyPluginInterface))]
Lazy<IMyPluginInterface, IPluginCapabilities>[] m_plugins;

though it also exhibits the same behavior with

 

 

[ImportMany(typeof(IMyPluginInterface))]
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.

 

[Export(typeof(IMyPluginInterface))]
[ExportMetadata("Name", "Plugin 1")]
[ExportMetadata("Description", "First plugin")]
[ExportMetadata("Version", "1.0.0.0")]
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.

Developer
Nov 12, 2009 at 12:43 AM

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 4: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.