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 ExportComponentManagerInheritance: object → ExportComponentManager
Remarks
Section titled “Remarks”All members are thread safe.
Constructors
Section titled “Constructors”ExportComponentManager()
Section titled “ExportComponentManager()”public ExportComponentManager()Properties
Section titled “Properties”AssemblyCacheDirectory
Section titled “AssemblyCacheDirectory”Gets the directory with assembly cache files, or null when the cache is not used.
public string AssemblyCacheDirectory { get; }Property Value
Section titled “Property Value”Methods
Section titled “Methods”AddNotify(Type, EventHandler)
Section titled “AddNotify(Type, EventHandler)”Adds a handler called when the exports of the given contract type change.
public void AddNotify(Type contractType, EventHandler handler)Parameters
Section titled “Parameters”contractTypeType
Contract type to watch.handlerEventHandler
Handler to add.
CreateExportInstance<T>(Uri)
Section titled “CreateExportInstance<T>(Uri)”Creates a new instance of the export with the given location.
public T CreateExportInstance(Uri location)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”T
New instance, or the default value of T when no such export exists.
CreateExportInstance(Uri)
Section titled “CreateExportInstance(Uri)”Creates a new instance of the export with the given location.
public object CreateExportInstance(Uri location)Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”New instance, or the default value of T when no such export exists.
CreateExportInstances<T>()
Section titled “CreateExportInstances<T>()”Creates a new instance of every export of the given contract.
public IEnumerable<T> CreateExportInstances()Type Parameters
Section titled “Type Parameters”T
Contract type.
Returns
Section titled “Returns”IEnumerable<T>
New instances of exports without a contract name.
CreateExportInstances<T>(string)
Section titled “CreateExportInstances<T>(string)”Creates a new instance of every export of the given contract.
public IEnumerable<T> CreateExportInstances(string contractName)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamestring
Contract name, ornullfor exports without a contract name.
Returns
Section titled “Returns”IEnumerable<T>
New instances.
CreateExportInstances(ContractDefinition)
Section titled “CreateExportInstances(ContractDefinition)”Creates a new instance of every export of the given contract.
public IEnumerable<object> CreateExportInstances(ContractDefinition contract)Parameters
Section titled “Parameters”contractContractDefinition
Returns
Section titled “Returns”New instances.
CreateExportInstances(Type)
Section titled “CreateExportInstances(Type)”Creates a new instance of every export of the given contract.
public IEnumerable<object> CreateExportInstances(Type contractType)Parameters
Section titled “Parameters”contractTypeType
Returns
Section titled “Returns”New instances.
CreateExportInstances(string, Type)
Section titled “CreateExportInstances(string, Type)”Creates a new instance of every export of the given contract.
public IEnumerable<object> CreateExportInstances(string contractName, Type contractType)Parameters
Section titled “Parameters”Returns
Section titled “Returns”New instances.
CreateManyExportInstances<T>(string[])
Section titled “CreateManyExportInstances<T>(string[])”Creates a new instance of every export of the given contract names.
public IEnumerable<T> CreateManyExportInstances(string[] contractNames)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.
Returns
Section titled “Returns”IEnumerable<T>
New instances.
CreateManyExportInstances(string[], Type)
Section titled “CreateManyExportInstances(string[], Type)”Creates a new instance of every export of the given contract names.
public IEnumerable<object> CreateManyExportInstances(string[] contractNames, Type contractType)Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.contractTypeType
Returns
Section titled “Returns”New instances.
GetExport<T>(Uri)
Section titled “GetExport<T>(Uri)”Gets the export with the given location.
public ExportComponent<T> GetExport(Uri location)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”Export component, or null when no such export exists or its contract type differs.
GetExport(Uri)
Section titled “GetExport(Uri)”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)Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”Export component, or null when no such export exists.
GetExportInstance<T>(Uri)
Section titled “GetExportInstance<T>(Uri)”Gets the shared instance of the export with the given location.
public T GetExportInstance(Uri location)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”T
Shared instance, or the default value of T when no such export exists.
GetExportInstance(Uri)
Section titled “GetExportInstance(Uri)”Gets the shared instance of the export with the given location.
public object GetExportInstance(Uri location)Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”Shared instance, or the default value of T when no such export exists.
GetExportInstances<T>()
Section titled “GetExportInstances<T>()”Gets the shared instances of all exports of the given contract.
public IEnumerable<T> GetExportInstances()Type Parameters
Section titled “Type Parameters”T
Contract type.
Returns
Section titled “Returns”IEnumerable<T>
Shared instances of exports without a contract name.
GetExportInstances<T>(string)
Section titled “GetExportInstances<T>(string)”Gets the shared instances of all exports of the given contract.
public IEnumerable<T> GetExportInstances(string contractName)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamestring
Contract name, ornullfor exports without a contract name.
Returns
Section titled “Returns”IEnumerable<T>
Shared instances.
GetExportInstances(ContractDefinition)
Section titled “GetExportInstances(ContractDefinition)”Gets the shared instances of all exports of the given contract.
public IEnumerable<object> GetExportInstances(ContractDefinition contract)Parameters
Section titled “Parameters”contractContractDefinition
Returns
Section titled “Returns”Shared instances.
GetExportInstances(Type)
Section titled “GetExportInstances(Type)”Gets the shared instances of all exports of the given contract.
public IEnumerable<object> GetExportInstances(Type contractType)Parameters
Section titled “Parameters”contractTypeType
Returns
Section titled “Returns”Shared instances.
GetExportInstances(string, Type)
Section titled “GetExportInstances(string, Type)”Gets the shared instances of all exports of the given contract.
public IEnumerable<object> GetExportInstances(string contractName, Type contractType)Parameters
Section titled “Parameters”Returns
Section titled “Returns”Shared instances.
GetExports<T>()
Section titled “GetExports<T>()”Gets all exports of the given contract.
public IEnumerable<ExportComponent<T>> GetExports()Type Parameters
Section titled “Type Parameters”T
Contract type.
Returns
Section titled “Returns”IEnumerable<ExportComponent<T>>
Exports without a contract name.
GetExports<T>(string)
Section titled “GetExports<T>(string)”Gets all exports of the given contract.
public IEnumerable<ExportComponent<T>> GetExports(string contractName)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamestring
Contract name, ornullfor exports without a contract name.
Returns
Section titled “Returns”IEnumerable<ExportComponent<T>>
Exports.
GetExports(ContractDefinition)
Section titled “GetExports(ContractDefinition)”Gets all exports of the given contract.
public IEnumerable<ExportComponent> GetExports(ContractDefinition contract)Parameters
Section titled “Parameters”contractContractDefinition
Returns
Section titled “Returns”Exports.
GetExports(Type)
Section titled “GetExports(Type)”Gets all exports of the given contract.
public IEnumerable<ExportComponent> GetExports(Type contractType)Parameters
Section titled “Parameters”contractTypeType
Returns
Section titled “Returns”Exports.
GetExports(string, Type)
Section titled “GetExports(string, Type)”Gets all exports of the given contract.
public IEnumerable<ExportComponent> GetExports(string contractName, Type contractType)Parameters
Section titled “Parameters”Returns
Section titled “Returns”Exports.
GetExports(Assembly)
Section titled “GetExports(Assembly)”Gets all exports that come from the given assembly.
public IList<ExportComponent> GetExports(Assembly assembly)Parameters
Section titled “Parameters”assemblyAssembly
Origin assembly.
Returns
Section titled “Returns”Exports.
GetExportsDefinitions(Assembly)
Section titled “GetExportsDefinitions(Assembly)”Gets the definitions of all exports whose value type comes from the given assembly.
public IList<ExportComponentDefinition> GetExportsDefinitions(Assembly assembly)Parameters
Section titled “Parameters”assemblyAssembly
Value type assembly.
Returns
Section titled “Returns”IList<ExportComponentDefinition>
Export definitions.
Remarks
Section titled “Remarks”Resolves the value type of every export, which loads types that the cache left unresolved.
GetLoadedAssemblies()
Section titled “GetLoadedAssemblies()”Gets the assemblies loaded into the container.
public Assembly[] GetLoadedAssemblies()Returns
Section titled “Returns”Loaded assemblies.
GetManyExportInstances<T>(string[])
Section titled “GetManyExportInstances<T>(string[])”Gets the shared instances of all exports of the given contract names.
public IEnumerable<T> GetManyExportInstances(string[] contractNames)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.
Returns
Section titled “Returns”IEnumerable<T>
Shared instances.
GetManyExportInstances(string[], Type)
Section titled “GetManyExportInstances(string[], Type)”Gets the shared instances of all exports of the given contract names.
public IEnumerable<object> GetManyExportInstances(string[] contractNames, Type contractType)Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.contractTypeType
Returns
Section titled “Returns”Shared instances.
GetManyExports<T>(string[])
Section titled “GetManyExports<T>(string[])”Gets all exports of the given contract names.
public IEnumerable<ExportComponent<T>> GetManyExports(string[] contractNames)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.
Returns
Section titled “Returns”IEnumerable<ExportComponent<T>>
Exports.
GetManyExports(string[], Type)
Section titled “GetManyExports(string[], Type)”Gets all exports of the given contract names.
public IList<ExportComponent> GetManyExports(string[] contractNames, Type contractType)Parameters
Section titled “Parameters”contractNamesstring[]
Contract names. Anullitem means exports without a contract name; duplicates are ignored.contractTypeType
Returns
Section titled “Returns”Exports.
LoadAssembly(Type)
Section titled “LoadAssembly(Type)”Loads all exports of the given assembly. Does nothing when the assembly is already loaded.
public void LoadAssembly(Type typeInRequestedAssembly)Parameters
Section titled “Parameters”typeInRequestedAssemblyType
Any type from the assembly to load.
Remarks
Section titled “Remarks”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.
LoadAssembly(Assembly)
Section titled “LoadAssembly(Assembly)”Loads all exports of the given assembly. Does nothing when the assembly is already loaded.
public void LoadAssembly(Assembly assembly)Parameters
Section titled “Parameters”assemblyAssembly
Assembly to load.
Remarks
Section titled “Remarks”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.
PrepareCache()
Section titled “PrepareCache()”Writes cache files for the assemblies that were loaded through reflection and deletes the files of their earlier builds.
public void PrepareCache()Exceptions
Section titled “Exceptions”- InvalidOperationException - UseCache(string, Func, Action) was not called.
Remarks
Section titled “Remarks”Call after startup, for example from a background task.
Register<T>(ExportComponent<T>)
Section titled “Register<T>(ExportComponent<T>)”Registers a component that no assembly declares.
public void Register(ExportComponent<T> component)Type Parameters
Section titled “Type Parameters”T
Contract type.
Parameters
Section titled “Parameters”componentExportComponent<T>
Component to register.
Exceptions
Section titled “Exceptions”- InvalidOperationException - An export with the same location is already registered.
RemoveNotify(Type, EventHandler)
Section titled “RemoveNotify(Type, EventHandler)”Removes a handler added by AddNotify(Type, EventHandler).
public void RemoveNotify(Type contractType, EventHandler handler)Parameters
Section titled “Parameters”contractTypeType
Watched contract type.handlerEventHandler
Handler to remove.
UnloadAssembly(Assembly)
Section titled “UnloadAssembly(Assembly)”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)Parameters
Section titled “Parameters”assemblyAssembly
Assembly to unload.
Unregister(Uri)
Section titled “Unregister(Uri)”Removes the export with the given location.
public bool Unregister(Uri location)Parameters
Section titled “Parameters”locationUri
Export URI.
Returns
Section titled “Returns”true when an export was removed.
Unregister(ExportComponent)
Section titled “Unregister(ExportComponent)”Removes a registered component. Exports that come from an assembly stay untouched.
public bool Unregister(ExportComponent component)Parameters
Section titled “Parameters”componentExportComponent
Component to remove.
Returns
Section titled “Returns”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)Parameters
Section titled “Parameters”assemblyCacheDirectorystring
Directory with the cache files.deserializeFromFileFunc<string, Type, object>
Reads an object of the given type from a file.serializeToFileAction<string, object>
Writes an object to a file.
Remarks
Section titled “Remarks”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.
UseCustomTypeResolver(Func<string, Type>)
Section titled “UseCustomTypeResolver(Func<string, Type>)”Replaces the resolver that turns a contract type name from a cache file into a type.
public void UseCustomTypeResolver(Func<string, Type> typeResolver)Parameters
Section titled “Parameters”typeResolverFunc<string, Type>
Resolver called with an assembly qualified type name. Default: Type.GetType(string).
Remarks
Section titled “Remarks”A resolver that returns null makes the container ignore the cache file and reflect over the assembly.
Events
Section titled “Events”ExportCollectionChanged
Section titled “ExportCollectionChanged”Occurs when exports are added or removed.
public EventHandler<ExportCollectionChangedEventArgs> ExportCollectionChanged