1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System
.Collections
.Generic
;
6 using System
.ComponentModel
.Composition
.Primitives
;
7 using System
.Diagnostics
.CodeAnalysis
;
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();
48 if (this._constructor
== null)
50 this._constructor
= constructor
;
55 return this._constructor
;
58 public override IEnumerable
<ExportDefinition
> ExportDefinitions
62 if (this._exports
== null)
64 ExportDefinition
[] exports
= this._creationInfo
.GetExports().ToArray();
67 if (this._exports
== null)
69 this._exports
= exports
;
77 public override IEnumerable
<ImportDefinition
> ImportDefinitions
81 if (this._imports
== null)
83 ImportDefinition
[] imports
= this._creationInfo
.GetImports().ToArray();
86 if (this._imports
== null)
88 this._imports
= imports
;
96 public override IDictionary
<string, object> Metadata
100 if (this._metadata
== null)
102 IDictionary
<string, object> metadata
= this._creationInfo
.GetMetadata().AsReadOnly();
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);
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
;