root / trunk / Pithos.Core / Agents / AgentLocator.cs @ c28a075a
History | View | Annotate | Download (1003 Bytes)
1 |
using System; |
---|---|
2 |
using System.Collections.Concurrent; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
|
7 |
namespace Pithos.Core.Agents |
8 |
{ |
9 |
static class AgentLocator<T> where T:class |
10 |
{ |
11 |
static ConcurrentDictionary<string, WeakReference> _agents = new ConcurrentDictionary<string, WeakReference>(); |
12 |
public static void Register(T agent,string key) |
13 |
{ |
14 |
_agents[key] = new WeakReference(agent); |
15 |
} |
16 |
|
17 |
public static T Get(string key) |
18 |
{ |
19 |
return _agents[key].Target as T; |
20 |
} |
21 |
|
22 |
public static bool TryGet(string key, out T value) |
23 |
{ |
24 |
WeakReference target; |
25 |
var exists = _agents.TryGetValue(key, out target); |
26 |
value = target.Target as T; |
27 |
return exists; |
28 |
} |
29 |
|
30 |
public static void Remove(string key) |
31 |
{ |
32 |
WeakReference target; |
33 |
_agents.TryRemove(key, out target); |
34 |
} |
35 |
} |
36 |
} |