Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Doesn't look like it can be done. If you'll notice from my test code, <code>RegistrationBuilder</code> won't even export a class that is decorated with a <code>ExportMetadata</code> attribute (message: System.ComponentModel.Composition Warning: 102 : An Export specification convention that would apply to type 'test.MyClassA' has been overridden by attributes applied in the source file or by a prior convention). MEF attributes take precedence over Fluent configuration via <code>RegistrationBuilder</code></p> <pre><code>class Program { static void Main() { // importing class instance var mcc = new MyClassConsumer(); // type to export var typeToExport = typeof(MyClassA); Console.WriteLine("Type to export: {0}", typeToExport); var rb = new RegistrationBuilder(); rb.ForType(typeToExport) .ExportInterfaces(); var catalog = new AssemblyCatalog(typeof(Program).Assembly, rb); var container = new CompositionContainer(catalog); container.ComposeParts(mcc); // bombs if MyClassA's MetadataAttribute is not commented out Console.WriteLine("Imported property's type: {0}", mcc.MyClass.GetType()); Console.ReadLine(); } } public enum MyClassType { TypeA, TypeB } public interface IClass {} public interface IClassMetaData { MyClassType Type { get; } } [ExportMetadata("Type", MyClassType.TypeA)] // works if you comment this line public class MyClassA : IClass { } [ExportMetadata("Type", MyClassType.TypeB)] public class MyClassB : IClass { } public class MyClassConsumer { [Import] public IClass MyClass { get; set; } } </code></pre> <p>UPDATE: if you can add <code>[Export(IClass)]</code> to the classes you'd like to export, you can filter the catalog as shown below:</p> <pre><code>class Program { static void Main() { var catalog = new AssemblyCatalog(typeof(Program).Assembly); var filteredCatalog = catalog.Filter(p =&gt; { var type = ReflectionModelServices.GetPartType(p).Value; return typeof(IClass).IsAssignableFrom( type ) &amp;&amp; // implements interface you're looking for Attribute.IsDefined(type, typeof(ExportMetadataAttribute)) &amp;&amp; // has ExportMetadata attribute // check for Type == MyClassType.TypeA type.GetCustomAttributes(typeof(ExportMetadataAttribute), true).Any(ca =&gt; { var ema = (ExportMetadataAttribute)ca; return ema.Name == "Type" &amp;&amp; (MyClassType)ema.Value == MyClassType.TypeA; }); }); var container = new CompositionContainer(filteredCatalog); MyClassConsumer mcc = new MyClassConsumer(); container.ComposeParts(mcc); Console.WriteLine("Imported property's type: {0}", mcc.MyClass.GetType()); Console.ReadLine(); } } public enum MyClassType { TypeA, TypeB } public interface IClass {} public interface IClassMetaData { MyClassType Type { get; } } [Export(typeof(IClass))] [ExportMetadata("Type", MyClassType.TypeA)] public class MyClassA : IClass { } [Export(typeof(IClass))] [ExportMetadata("Type", MyClassType.TypeB)] public class MyClassB : IClass { } public class MyClassConsumer { [Import] public IClass MyClass { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload