/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

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