2 // -----------------------------------------------------------------------
\r
3 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
4 // -----------------------------------------------------------------------
\r
6 using System.Collections.Generic;
\r
7 using System.ComponentModel.Composition.Hosting;
\r
8 using System.ComponentModel.Composition.Primitives;
\r
9 using System.Diagnostics.CodeAnalysis;
\r
10 using System.Globalization;
\r
12 using System.Reflection;
\r
13 using Microsoft.Internal;
\r
14 using Microsoft.Internal.Collections;
\r
15 using System.Threading;
\r
17 namespace System.ComponentModel.Composition.ReflectionModel
\r
19 internal sealed class DisposableReflectionComposablePart : ReflectionComposablePart, IDisposable
\r
21 private volatile int _isDisposed = 0;
\r
23 public DisposableReflectionComposablePart(ReflectionComposablePartDefinition definition)
\r
28 protected override void ReleaseInstanceIfNecessary(object instance)
\r
30 IDisposable disposable = instance as IDisposable;
\r
31 if (disposable != null)
\r
33 disposable.Dispose();
\r
37 protected override void EnsureRunning()
\r
39 base.EnsureRunning();
\r
40 if (this._isDisposed == 1)
\r
42 throw ExceptionBuilder.CreateObjectDisposed(this);
\r
46 void IDisposable.Dispose()
\r
48 // NOTE : According to http://msdn.microsoft.com/en-us/library/4bw5ewxy.aspx, the warning is bogus when used with Interlocked API.
\r
49 #pragma warning disable 420
\r
50 if (Interlocked.CompareExchange(ref this._isDisposed, 1, 0) == 0)
\r
51 #pragma warning restore 420
\r
53 this.ReleaseInstanceIfNecessary(this.CachedInstance);
\r