PageRenderTime 223ms CodeModel.GetById 161ms app.highlight 3ms RepoModel.GetById 58ms app.codeStats 0ms

/src/LinFu.AOP/IgnoredInstancesRegistry.cs

http://github.com/philiplaureano/LinFu
C# | 51 lines | 31 code | 7 blank | 13 comment | 4 complexity | 104b3fc9e6d646404be2a47e223f2d73 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3
 4namespace LinFu.AOP.Cecil
 5{
 6    /// <summary>
 7    ///     Represents a class that keeps track of the internal object instances that should be ignored
 8    ///     by the interception routines.
 9    /// </summary>
10    public static class IgnoredInstancesRegistry
11    {
12        private static readonly HashSet<int> _instances;
13        private static readonly object _lock = new object();
14
15        static IgnoredInstancesRegistry()
16        {
17            _instances = new HashSet<int>();
18        }
19
20        /// <summary>
21        ///     Determines whether or not the registry contains the given ignored object.
22        /// </summary>
23        /// <param name="target">The target object.</param>
24        /// <returns>Returns <c>true</c> if the object should be ignored; otherwise, it will return <c>false</c>.</returns>
25        public static bool Contains(object target)
26        {
27            if (target == null)
28                throw new ArgumentNullException("target");
29
30            var hash = target.GetHashCode();
31
32            return _instances.Contains(hash);
33        }
34
35        /// <summary>
36        ///     Adds an instance to the list of ignored instances.
37        /// </summary>
38        /// <param name="target">The target instance to be ignored by the interception routines.</param>
39        public static void AddInstance(object target)
40        {
41            if (target == null)
42                throw new ArgumentNullException("target");
43
44            lock (_lock)
45            {
46                var hash = target.GetHashCode();
47                _instances.Add(hash);
48            }
49        }
50    }
51}