Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.ComponentModel.Composition / src / ComponentModel / System / ComponentModel / Composition / ReflectionModel / ReflectionComposablePartDefinition.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 using System;\r
5 using System.Collections.Generic;\r
6 using System.ComponentModel.Composition.Primitives;\r
7 using System.Diagnostics.CodeAnalysis;\r
8 using System.Linq;\r
9 using System.Reflection;\r
10 using System.Threading;\r
11 using Microsoft.Internal;\r
12 \r
13 namespace System.ComponentModel.Composition.ReflectionModel\r
14 {\r
15     internal class ReflectionComposablePartDefinition : ComposablePartDefinition, ICompositionElement\r
16     {\r
17         private readonly IReflectionPartCreationInfo _creationInfo;\r
18 \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
24 \r
25         public ReflectionComposablePartDefinition(IReflectionPartCreationInfo creationInfo)\r
26         {\r
27             Assumes.NotNull(creationInfo);\r
28             this._creationInfo = creationInfo;\r
29         }\r
30 \r
31         public Type GetPartType()\r
32         {\r
33             return this._creationInfo.GetPartType();\r
34         }\r
35 \r
36         public Lazy<Type> GetLazyPartType()\r
37         {\r
38             return this._creationInfo.GetLazyPartType();\r
39         }\r
40 \r
41         public ConstructorInfo GetConstructor()\r
42         {\r
43             if (this._constructor == null)\r
44             {\r
45                 ConstructorInfo constructor = this._creationInfo.GetConstructor();\r
46                 lock (this._lock)\r
47                 {\r
48                     if (this._constructor == null)\r
49                     {\r
50                         this._constructor = constructor;\r
51                     }\r
52                 }\r
53             }\r
54 \r
55             return this._constructor;\r
56         }\r
57 \r
58         public override IEnumerable<ExportDefinition> ExportDefinitions\r
59         {\r
60             get\r
61             {\r
62                 if (this._exports == null)\r
63                 {\r
64                     ExportDefinition[] exports = this._creationInfo.GetExports().ToArray();\r
65                     lock (this._lock)\r
66                     {\r
67                         if (this._exports == null)\r
68                         {\r
69                             this._exports = exports;\r
70                         }\r
71                     }\r
72                 }\r
73                 return this._exports;\r
74             }\r
75         }\r
76 \r
77         public override IEnumerable<ImportDefinition> ImportDefinitions\r
78         {\r
79             get\r
80             {\r
81                 if (this._imports == null)\r
82                 {\r
83                     ImportDefinition[] imports = this._creationInfo.GetImports().ToArray();\r
84                     lock (this._lock)\r
85                     {\r
86                         if (this._imports == null)\r
87                         {\r
88                             this._imports = imports;\r
89                         }\r
90                     }\r
91                 }\r
92                 return this._imports;\r
93             }\r
94         }\r
95 \r
96         public override IDictionary<string, object> Metadata\r
97         {\r
98             get\r
99             {\r
100                 if (this._metadata == null)\r
101                 {\r
102                     IDictionary<string, object> metadata = this._creationInfo.GetMetadata().AsReadOnly();\r
103                     lock (this._lock)\r
104                     {\r
105                         if (this._metadata == null)\r
106                         {\r
107                             this._metadata = metadata;\r
108                         }\r
109                     }\r
110                 }\r
111                 return this._metadata;\r
112             }\r
113         }\r
114 \r
115         internal bool IsDisposalRequired\r
116         {\r
117             get\r
118             {\r
119                 return this._creationInfo.IsDisposalRequired;\r
120             }\r
121         }\r
122 \r
123         [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]\r
124         public override ComposablePart CreatePart()\r
125         {\r
126             if (this.IsDisposalRequired)\r
127             {\r
128                 return new DisposableReflectionComposablePart(this);\r
129             }\r
130             else\r
131             {\r
132                 return new ReflectionComposablePart(this);\r
133             }\r
134         }\r
135 \r
136         string ICompositionElement.DisplayName\r
137         {\r
138             get { return this._creationInfo.DisplayName; }\r
139         }\r
140 \r
141         ICompositionElement ICompositionElement.Origin\r
142         {\r
143             get { return this._creationInfo.Origin; }\r
144         }\r
145 \r
146         public override string ToString()\r
147         {\r
148             return this._creationInfo.DisplayName;\r
149         }\r
150     }\r
151 }\r