Registering instances using MEF

I need to register an instance of an object in a container. I cannot use the generic ComposeExportedValue<T> since I do not know what T is at compile time.

I need something like this: container.RegisterInstance (type someType, an instance of the object)

Any ideas?

+4
source share
1 answer

Here is a version of ComposeExportedValue that is not generic:

 public static void ComposeExportedValue(this CompositionContainer container, string contractName, object exportedValue) { if (container == null) throw new ArgumentNullException("container"); if (exportedValue == null) throw new ArgumentNullException("exportedValue"); CompositionBatch batch = new CompositionBatch(); var metadata = new Dictionary<string, object> { { "ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(exportedValue.GetType()) } }; batch.AddExport(new Export(contractName, metadata, () => exportedValue)); container.Compose(batch); } public static void ComposeExportedValue(this CompositionContainer container, object exportedValue) { if (container == null) throw new ArgumentNullException("container"); if (exportedValue == null) throw new ArgumentNullException("exportedValue"); ComposeExportedValue(container, AttributedModelServices.GetContractName(exportedValue.GetType(), exportedValue); } 
+2
source

Source: https://habr.com/ru/post/1309902/


All Articles