1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.ComponentModel.Composition.Hosting;
\r
6 using System.ComponentModel.Composition.Primitives;
\r
7 using System.Globalization;
\r
8 using System.Reflection;
\r
9 using Microsoft.Internal;
\r
10 using System.Threading;
\r
12 namespace System.ComponentModel.Composition.ReflectionModel
\r
14 internal class ExportingMember
\r
16 private readonly ExportDefinition _definition;
\r
17 private readonly ReflectionMember _member;
\r
18 private object _cachedValue = null;
\r
19 private volatile bool _isValueCached = false;
\r
21 public ExportingMember(ExportDefinition definition, ReflectionMember member)
\r
23 Assumes.NotNull(definition, member);
\r
25 this._definition = definition;
\r
26 this._member = member;
\r
29 public bool RequiresInstance
\r
31 get { return _member.RequiresInstance; }
\r
34 public ExportDefinition Definition
\r
36 get { return _definition; }
\r
39 public object GetExportedValue(object instance, object @lock)
\r
41 this.EnsureReadable();
\r
43 if (!this._isValueCached)
\r
45 object exportedValue;
\r
48 exportedValue = this._member.GetValue(instance);
\r
50 catch (TargetInvocationException exception)
\r
51 { // Member threw an exception. Avoid letting this
\r
52 // leak out as a 'raw' unhandled exception, instead,
\r
53 // we'll add some context and rethrow.
\r
55 throw new ComposablePartException(
\r
56 CompositionErrorId.ReflectionModel_ExportThrewException,
\r
57 String.Format(CultureInfo.CurrentCulture,
\r
58 Strings.ReflectionModel_ExportThrewException,
\r
59 this._member.GetDisplayName()),
\r
60 Definition.ToElement(),
\r
61 exception.InnerException);
\r
66 if (!this._isValueCached)
\r
68 this._cachedValue = exportedValue;
\r
69 Thread.MemoryBarrier();
\r
71 this._isValueCached = true;
\r
76 return this._cachedValue;
\r
79 private void EnsureReadable()
\r
81 if (!this._member.CanRead)
\r
82 { // Property does not have a getter
\r
84 throw new ComposablePartException(
\r
85 CompositionErrorId.ReflectionModel_ExportNotReadable,
\r
86 String.Format(CultureInfo.CurrentCulture,
\r
87 Strings.ReflectionModel_ExportNotReadable,
\r
88 this._member.GetDisplayName()),
\r
89 Definition.ToElement());
\r