Freigeben über


ExportImplementationAttribute Class

Definition

Along with ImportImplementationsAttribute enables MEF proxy pattern where a single component export serves as a proxy for the best implementation selected at run time. This pattern allows component consumers to just [Import] it, hiding the complexity of selecting one of implementations.

public ref class ExportImplementationAttribute sealed : System::ComponentModel::Composition::ExportAttribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class ExportImplementationAttribute : System.ComponentModel.Composition.ExportAttribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)>]
type ExportImplementationAttribute = class
    inherit ExportAttribute
Public NotInheritable Class ExportImplementationAttribute
Inherits ExportAttribute
Inheritance
ExportImplementationAttribute
Attributes

Examples

A typical sample:

A component contract definition:

interface IService {
    void Foo();
}

Default implementation:

[ExportImplementation(typeof(IService))]
[Name("default")]
class DefaultService : IService {...}

Another implementation:

[ExportImplementation(typeof(IService))]
[Name("A better implementation")]
[Order(Before = "default")]
class AdvancedService : IService {...}

A proxy:

[Export(typeof(IService))]
class ProxyService : IService {
   [ImportImplementations(typeof(IService))]
   IEnumerable<Lazy<IService, IOrderable>> _unorderedImplementations;

   public void Foo() {
       Orderer.Order(_unorderedImplementations).FirstOrDefault()?.Value.Foo();
   }
}

Consuming IService:

[Import]
IService service = null;

Constructors

ExportImplementationAttribute(Type)

Creates new ExportImplementationAttribute instance.

Applies to