Skip to content
Auto
Products

ExportComponentManager

Namespace: TagBites.ComponentModel.Composition
Assembly: TagBites.ComponentModel.Composition.dll
Product: TagBites.ComponentModel.Composition v. 1.2.1

Container of exported components. Discovers types marked with ExportAttribute and creates their instances on demand.

public class ExportComponentManager

Inheritance: object → ExportComponentManager

All members are thread safe.

public ExportComponentManager()

Gets the directory with assembly cache files, or null when the cache is not used.

public string AssemblyCacheDirectory { get; }

string

Adds a handler called when the exports of the given contract type change.

public void AddNotify(Type contractType, EventHandler handler)
  • contractType Type
    Contract type to watch.
  • handler EventHandler
    Handler to add.

Creates a new instance of the export with the given location.

public T CreateExportInstance(Uri location)
  • T
    Contract type.
  • location Uri
    Export URI.

T

New instance, or the default value of T when no such export exists.

Creates a new instance of the export with the given location.

public object CreateExportInstance(Uri location)
  • location Uri
    Export URI.

object

New instance, or the default value of T when no such export exists.

Creates a new instance of every export of the given contract.

public IEnumerable<T> CreateExportInstances()
  • T
    Contract type.

IEnumerable<T>

New instances of exports without a contract name.

Creates a new instance of every export of the given contract.

public IEnumerable<T> CreateExportInstances(string contractName)
  • T
    Contract type.
  • contractName string
    Contract name, or null for exports without a contract name.

IEnumerable<T>

New instances.

Creates a new instance of every export of the given contract.

public IEnumerable<object> CreateExportInstances(ContractDefinition contract)

IEnumerable<object>

New instances.

Creates a new instance of every export of the given contract.

public IEnumerable<object> CreateExportInstances(Type contractType)

IEnumerable<object>

New instances.

Creates a new instance of every export of the given contract.

public IEnumerable<object> CreateExportInstances(string contractName, Type contractType)
  • contractName string
    Contract name, or null for exports without a contract name.
  • contractType Type

IEnumerable<object>

New instances.

Creates a new instance of every export of the given contract names.

public IEnumerable<T> CreateManyExportInstances(string[] contractNames)
  • T
    Contract type.
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.

IEnumerable<T>

New instances.

Creates a new instance of every export of the given contract names.

public IEnumerable<object> CreateManyExportInstances(string[] contractNames, Type contractType)
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.
  • contractType Type

IEnumerable<object>

New instances.

Gets the export with the given location.

public ExportComponent<T> GetExport(Uri location)
  • T
    Contract type.
  • location Uri
    Export URI.

ExportComponent<T>

Export component, or null when no such export exists or its contract type differs.

Gets the export with the given location. Follows the override chain, so the last export registered for the location wins.

public ExportComponent GetExport(Uri location)
  • location Uri
    Export URI.

ExportComponent

Export component, or null when no such export exists.

Gets the shared instance of the export with the given location.

public T GetExportInstance(Uri location)
  • T
    Contract type.
  • location Uri
    Export URI.

T

Shared instance, or the default value of T when no such export exists.

Gets the shared instance of the export with the given location.

public object GetExportInstance(Uri location)
  • location Uri
    Export URI.

object

Shared instance, or the default value of T when no such export exists.

Gets the shared instances of all exports of the given contract.

public IEnumerable<T> GetExportInstances()
  • T
    Contract type.

IEnumerable<T>

Shared instances of exports without a contract name.

Gets the shared instances of all exports of the given contract.

public IEnumerable<T> GetExportInstances(string contractName)
  • T
    Contract type.
  • contractName string
    Contract name, or null for exports without a contract name.

IEnumerable<T>

Shared instances.

Gets the shared instances of all exports of the given contract.

public IEnumerable<object> GetExportInstances(ContractDefinition contract)

IEnumerable<object>

Shared instances.

Gets the shared instances of all exports of the given contract.

public IEnumerable<object> GetExportInstances(Type contractType)

IEnumerable<object>

Shared instances.

Gets the shared instances of all exports of the given contract.

public IEnumerable<object> GetExportInstances(string contractName, Type contractType)
  • contractName string
    Contract name, or null for exports without a contract name.
  • contractType Type

IEnumerable<object>

Shared instances.

Gets all exports of the given contract.

public IEnumerable<ExportComponent<T>> GetExports()
  • T
    Contract type.

IEnumerable<ExportComponent<T>>

Exports without a contract name.

Gets all exports of the given contract.

public IEnumerable<ExportComponent<T>> GetExports(string contractName)
  • T
    Contract type.
  • contractName string
    Contract name, or null for exports without a contract name.

IEnumerable<ExportComponent<T>>

Exports.

Gets all exports of the given contract.

public IEnumerable<ExportComponent> GetExports(ContractDefinition contract)

IEnumerable<ExportComponent>

Exports.

Gets all exports of the given contract.

public IEnumerable<ExportComponent> GetExports(Type contractType)

IEnumerable<ExportComponent>

Exports.

Gets all exports of the given contract.

public IEnumerable<ExportComponent> GetExports(string contractName, Type contractType)
  • contractName string
    Contract name, or null for exports without a contract name.
  • contractType Type

IEnumerable<ExportComponent>

Exports.

Gets all exports that come from the given assembly.

public IList<ExportComponent> GetExports(Assembly assembly)

IList<ExportComponent>

Exports.

Gets the definitions of all exports whose value type comes from the given assembly.

public IList<ExportComponentDefinition> GetExportsDefinitions(Assembly assembly)
  • assembly Assembly
    Value type assembly.

IList<ExportComponentDefinition>

Export definitions.

Resolves the value type of every export, which loads types that the cache left unresolved.

Gets the assemblies loaded into the container.

public Assembly[] GetLoadedAssemblies()

Assembly[]

Loaded assemblies.

Gets the shared instances of all exports of the given contract names.

public IEnumerable<T> GetManyExportInstances(string[] contractNames)
  • T
    Contract type.
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.

IEnumerable<T>

Shared instances.

Gets the shared instances of all exports of the given contract names.

public IEnumerable<object> GetManyExportInstances(string[] contractNames, Type contractType)
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.
  • contractType Type

IEnumerable<object>

Shared instances.

Gets all exports of the given contract names.

public IEnumerable<ExportComponent<T>> GetManyExports(string[] contractNames)
  • T
    Contract type.
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.

IEnumerable<ExportComponent<T>>

Exports.

Gets all exports of the given contract names.

public IList<ExportComponent> GetManyExports(string[] contractNames, Type contractType)
  • contractNames string[]
    Contract names. A null item means exports without a contract name; duplicates are ignored.
  • contractType Type

IList<ExportComponent>

Exports.

Loads all exports of the given assembly. Does nothing when the assembly is already loaded.

public void LoadAssembly(Type typeInRequestedAssembly)
  • typeInRequestedAssembly Type
    Any type from the assembly to load.

Reads the export definitions from the cache when a matching cache file exists, otherwise reflects over all types of the assembly. Either all exports of the assembly become available, or none: a failure unloads the assembly and rethrows.

Loads all exports of the given assembly. Does nothing when the assembly is already loaded.

public void LoadAssembly(Assembly assembly)

Reads the export definitions from the cache when a matching cache file exists, otherwise reflects over all types of the assembly. Either all exports of the assembly become available, or none: a failure unloads the assembly and rethrows.

Writes cache files for the assemblies that were loaded through reflection and deletes the files of their earlier builds.

public void PrepareCache()

Call after startup, for example from a background task.

Registers a component that no assembly declares.

public void Register(ExportComponent<T> component)
  • T
    Contract type.

Removes a handler added by AddNotify(Type, EventHandler).

public void RemoveNotify(Type contractType, EventHandler handler)
  • contractType Type
    Watched contract type.
  • handler EventHandler
    Handler to remove.

Removes all exports of the given assembly and restores the exports it replaced. Does nothing when the assembly is not loaded.

public void UnloadAssembly(Assembly assembly)

Removes the export with the given location.

public bool Unregister(Uri location)
  • location Uri
    Export URI.

bool

true when an export was removed.

Removes a registered component. Exports that come from an assembly stay untouched.

public bool Unregister(ExportComponent component)

bool

true when the component was removed.

UseCache(string, Func<string, Type, object>, Action<string, object>)

Section titled “UseCache(string, Func<string, Type, object>, Action<string, object>)”

Turns on the assembly cache, which replaces reflection over all types with reading one file per assembly.

public void UseCache(string assemblyCacheDirectory, Func<string, Type, object> deserializeFromFile, Action<string, object> serializeToFile)
  • assemblyCacheDirectory string
    Directory with the cache files.
  • deserializeFromFile Func<string, Type, object>
    Reads an object of the given type from a file.
  • serializeToFile Action<string, object>
    Writes an object to a file.

Call before the first LoadAssembly(Assembly). A cache file belongs to one build of an assembly, so a rebuilt assembly is read through reflection again. An unreadable file falls back to reflection as well. A process that can write to the directory decides which types the container creates, so keep the directory out of reach of untrusted users.

Replaces the resolver that turns a contract type name from a cache file into a type.

public void UseCustomTypeResolver(Func<string, Type> typeResolver)

A resolver that returns null makes the container ignore the cache file and reflect over the assembly.

Occurs when exports are added or removed.

public EventHandler<ExportCollectionChangedEventArgs> ExportCollectionChanged

EventHandler<ExportCollectionChangedEventArgs>