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.Diagnostics.CodeAnalysis;
\r
9 using System.Reflection;
\r
10 using System.Threading;
\r
11 using Microsoft.Internal;
\r
13 namespace System.ComponentModel.Composition.ReflectionModel
\r
15 internal class ReflectionComposablePartDefinition : ComposablePartDefinition, ICompositionElement
\r
17 private readonly IReflectionPartCreationInfo _creationInfo;
\r
19 private volatile IEnumerable<ImportDefinition> _imports;
\r
20 private volatile IEnumerable<ExportDefinition> _exports;
\r
21 private volatile IDictionary<string, object> _metadata;
\r
22 private volatile ConstructorInfo _constructor;
\r
23 private object _lock = new object();
\r
25 public ReflectionComposablePartDefinition(IReflectionPartCreationInfo creationInfo)
\r
27 Assumes.NotNull(creationInfo);
\r
28 this._creationInfo = creationInfo;
\r
31 public Type GetPartType()
\r
33 return this._creationInfo.GetPartType();
\r
36 public Lazy<Type> GetLazyPartType()
\r
38 return this._creationInfo.GetLazyPartType();
\r
41 public ConstructorInfo GetConstructor()
\r
43 if (this._constructor == null)
\r
45 ConstructorInfo constructor = this._creationInfo.GetConstructor();
\r
48 if (this._constructor == null)
\r
50 this._constructor = constructor;
\r
55 return this._constructor;
\r
58 public override IEnumerable<ExportDefinition> ExportDefinitions
\r
62 if (this._exports == null)
\r
64 ExportDefinition[] exports = this._creationInfo.GetExports().ToArray();
\r
67 if (this._exports == null)
\r
69 this._exports = exports;
\r
73 return this._exports;
\r
77 public override IEnumerable<ImportDefinition> ImportDefinitions
\r
81 if (this._imports == null)
\r
83 ImportDefinition[] imports = this._creationInfo.GetImports().ToArray();
\r
86 if (this._imports == null)
\r
88 this._imports = imports;
\r
92 return this._imports;
\r
96 public override IDictionary<string, object> Metadata
\r
100 if (this._metadata == null)
\r
102 IDictionary<string, object> metadata = this._creationInfo.GetMetadata().AsReadOnly();
\r
105 if (this._metadata == null)
\r
107 this._metadata = metadata;
\r
111 return this._metadata;
\r
115 internal bool IsDisposalRequired
\r
119 return this._creationInfo.IsDisposalRequired;
\r
123 [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
\r
124 public override ComposablePart CreatePart()
\r
126 if (this.IsDisposalRequired)
\r
128 return new DisposableReflectionComposablePart(this);
\r
132 return new ReflectionComposablePart(this);
\r
136 string ICompositionElement.DisplayName
\r
138 get { return this._creationInfo.DisplayName; }
\r
141 ICompositionElement ICompositionElement.Origin
\r
143 get { return this._creationInfo.Origin; }
\r
146 public override string ToString()
\r
148 return this._creationInfo.DisplayName;
\r