Fixing an issue with output parameters that are of type IntPtr
[castle.git] / InversionOfControl / Castle.MicroKernel / Lifestyle / PerThreadLifestyleManager.cs
blob88669768ccddfb7f6928c8bda3feacc2361c5d15
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MicroKernel.Lifestyle
17 using System;
18 using System.Collections;
19 using System.Threading;
20 using System.Runtime.Serialization;
22 /// <summary>
23 /// Summary description for PerThreadLifestyleManager.
24 /// </summary>
25 [Serializable]
26 public class PerThreadLifestyleManager : AbstractLifestyleManager, IDeserializationCallback
28 [NonSerialized]
29 private static LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot("CastlePerThread");
31 [NonSerialized]
32 private IList instances = new ArrayList();
34 /// <summary>
35 ///
36 /// </summary>
37 public override void Dispose()
39 foreach( object instance in instances )
41 base.Release( instance );
44 instances.Clear();
46 Thread.FreeNamedDataSlot( "CastlePerThread" );
49 #region IResolver Members
51 public override object Resolve(CreationContext context)
53 lock(slot)
55 Hashtable map = (Hashtable) Thread.GetData( slot );
57 if (map == null)
59 map = new Hashtable();
61 Thread.SetData( slot, map );
64 Object instance = map[ ComponentActivator ];
66 if ( instance == null )
68 instance = base.Resolve(context);
69 map.Add( ComponentActivator, instance );
70 instances.Add( instance );
73 return instance;
77 public override void Release(object instance)
79 // Do nothing.
82 #endregion
84 public void OnDeserialization(object sender)
86 slot = Thread.AllocateNamedDataSlot("CastlePerThread");
87 instances = new ArrayList();