1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition.Primitives;
\r
7 using System.ComponentModel.Composition.ReflectionModel;
\r
10 namespace System.ComponentModel.Composition.Hosting
\r
12 public partial class CatalogExportProvider
\r
14 internal class PartCreatorExport : Export
\r
16 private readonly CatalogExportProvider _catalogExportProvider;
\r
17 private readonly ComposablePartDefinition _partDefinition;
\r
18 private readonly ExportDefinition _exportDefinition;
\r
19 private ExportDefinition _partCreatorExportDefinition;
\r
20 private PartCreatorPartDefinition _partCreatorPartDefinition;
\r
22 public PartCreatorExport(CatalogExportProvider catalogExportProvider, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition)
\r
24 this._catalogExportProvider = catalogExportProvider;
\r
25 this._partDefinition = partDefinition;
\r
26 this._exportDefinition = exportDefinition;
\r
27 this._partCreatorExportDefinition = new PartCreatorExportDefinition(this._exportDefinition);
\r
30 public override ExportDefinition Definition
\r
32 get { return this._partCreatorExportDefinition; }
\r
35 protected override object GetExportedValueCore()
\r
37 if (this._partCreatorPartDefinition == null)
\r
39 this._partCreatorPartDefinition = new PartCreatorPartDefinition(this);
\r
41 return this._partCreatorPartDefinition;
\r
44 public Export CreateExportProduct()
\r
46 return new NonSharedCatalogExport(this._catalogExportProvider, this._partDefinition, this._exportDefinition);
\r
49 private class PartCreatorPartDefinition : ComposablePartDefinition
\r
51 private readonly PartCreatorExport _partCreatorExport;
\r
53 public PartCreatorPartDefinition(PartCreatorExport partCreatorExport)
\r
55 this._partCreatorExport = partCreatorExport;
\r
58 public override IEnumerable<ExportDefinition> ExportDefinitions
\r
60 get { return new ExportDefinition[] { this._partCreatorExport.Definition }; }
\r
63 public override IEnumerable<ImportDefinition> ImportDefinitions
\r
65 get { return Enumerable.Empty<ImportDefinition>(); }
\r
68 public ExportDefinition PartCreatorExportDefinition
\r
70 get { return this._partCreatorExport.Definition; }
\r
73 public Export CreateProductExport()
\r
75 return this._partCreatorExport.CreateExportProduct();
\r
78 public override ComposablePart CreatePart()
\r
80 return new PartCreatorPart(this);
\r
84 private sealed class PartCreatorPart : ComposablePart, IDisposable
\r
86 private readonly PartCreatorPartDefinition _definition;
\r
87 private readonly Export _export;
\r
89 public PartCreatorPart(PartCreatorPartDefinition definition)
\r
91 this._definition = definition;
\r
92 this._export = definition.CreateProductExport();
\r
95 public override IEnumerable<ExportDefinition> ExportDefinitions
\r
97 get { return this._definition.ExportDefinitions; }
\r
100 public override IEnumerable<ImportDefinition> ImportDefinitions
\r
102 get { return this._definition.ImportDefinitions; }
\r
105 public override object GetExportedValue(ExportDefinition definition)
\r
107 if (definition != this._definition.PartCreatorExportDefinition)
\r
109 throw ExceptionBuilder.CreateExportDefinitionNotOnThisComposablePart("definition");
\r
112 return this._export.Value;
\r
115 public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
\r
117 throw ExceptionBuilder.CreateImportDefinitionNotOnThisComposablePart("definition");
\r
120 public void Dispose()
\r
122 IDisposable disposable = this._export as IDisposable;
\r
124 if (disposable != null)
\r
126 disposable.Dispose();
\r