update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / ReflectionModel / ReflectionComposablePartDefinition.cs
blob347fa2ed8fbe161abac28ad0d9c39e15dc53ad99
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel.Composition.Primitives;
7 using System.Diagnostics.CodeAnalysis;
8 using System.Linq;
9 using System.Reflection;
10 using System.Threading;
11 using Microsoft.Internal;
13 namespace System.ComponentModel.Composition.ReflectionModel
15 internal class ReflectionComposablePartDefinition : ComposablePartDefinition, ICompositionElement
17 private readonly IReflectionPartCreationInfo _creationInfo;
19 private volatile IEnumerable<ImportDefinition> _imports;
20 private volatile IEnumerable<ExportDefinition> _exports;
21 private volatile IDictionary<string, object> _metadata;
22 private volatile ConstructorInfo _constructor;
23 private object _lock = new object();
25 public ReflectionComposablePartDefinition(IReflectionPartCreationInfo creationInfo)
27 Assumes.NotNull(creationInfo);
28 this._creationInfo = creationInfo;
31 public Type GetPartType()
33 return this._creationInfo.GetPartType();
36 public Lazy<Type> GetLazyPartType()
38 return this._creationInfo.GetLazyPartType();
41 public ConstructorInfo GetConstructor()
43 if (this._constructor == null)
45 ConstructorInfo constructor = this._creationInfo.GetConstructor();
46 lock (this._lock)
48 if (this._constructor == null)
50 this._constructor = constructor;
55 return this._constructor;
58 public override IEnumerable<ExportDefinition> ExportDefinitions
60 get
62 if (this._exports == null)
64 ExportDefinition[] exports = this._creationInfo.GetExports().ToArray();
65 lock (this._lock)
67 if (this._exports == null)
69 this._exports = exports;
73 return this._exports;
77 public override IEnumerable<ImportDefinition> ImportDefinitions
79 get
81 if (this._imports == null)
83 ImportDefinition[] imports = this._creationInfo.GetImports().ToArray();
84 lock (this._lock)
86 if (this._imports == null)
88 this._imports = imports;
92 return this._imports;
96 public override IDictionary<string, object> Metadata
98 get
100 if (this._metadata == null)
102 IDictionary<string, object> metadata = this._creationInfo.GetMetadata().AsReadOnly();
103 lock (this._lock)
105 if (this._metadata == null)
107 this._metadata = metadata;
111 return this._metadata;
115 internal bool IsDisposalRequired
119 return this._creationInfo.IsDisposalRequired;
123 [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
124 public override ComposablePart CreatePart()
126 if (this.IsDisposalRequired)
128 return new DisposableReflectionComposablePart(this);
130 else
132 return new ReflectionComposablePart(this);
136 string ICompositionElement.DisplayName
138 get { return this._creationInfo.DisplayName; }
141 ICompositionElement ICompositionElement.Origin
143 get { return this._creationInfo.Origin; }
146 public override string ToString()
148 return this._creationInfo.DisplayName;