Im trying to export all classes which implement an IJob
interface while also passing metadata at the individual class level. What I've tried:
Export:
[InheritedExport(typeof(IJob))]
public interface IJob
{
int Run();
}
Import:
[ImportMany]
public IEnumerable<Lazy<IJob, IJobMetaData>> Jobs { get; set; }
Implementation:
[IgnoreJob(false)]
public class MyJob : IJob
{
public int Run()
{
return 5;
}
}
Attribute setup:
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class IgnoreJobAttribute : ExportAttribute, IJobMetaData
{
public IgnoreJobAttribute(bool ignore)
: base(typeof(IJobMetaData))
{
Ignore = ignore;
}
[DefaultValue(true)]
public bool Ignore { get; set; }
}
The above does not pass my metadata but if I remove the InheritedExport
attribute and add an Export
attribute to the individual implementation of IJob it works fine...
Take a look at this: How to use MEF Inherited Export & MetaData?
It turns out that the metadata associated with your export must be available when the InheritedExport is being registered. MEF treats all exports that are made after inheriting from the given class as the same kind of export. Therefore, your metadata that is defined in the subclass is ignored.
So you are not able to use InheritedExport in this case, I'm afraid.
As I can see, you already inherit from ExportAttribute
in your IgnoreJobAttribute
, therefore, you should be able to drop the InheritedExportAttribute
on the base class, as long as you keep a IgnoreJobAttribute
on every IJob you define.