PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/Builders/ProviderCache.cs

#
C# | 62 lines | 46 code | 11 blank | 5 comment | 6 complexity | 18343dfe9df442026873efeac421bf8d MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Text;
  9. namespace NUnit.Core.Builders
  10. {
  11. class ProviderCache
  12. {
  13. private static IDictionary instances = new Hashtable();
  14. public static object GetInstanceOf(Type providerType)
  15. {
  16. CacheEntry entry = new CacheEntry(providerType);
  17. object instance = instances[entry];
  18. return instance == null
  19. ? instances[entry] = Reflect.Construct(providerType)
  20. : instance;
  21. }
  22. public static void Clear()
  23. {
  24. foreach (object key in instances.Keys)
  25. {
  26. IDisposable provider = instances[key] as IDisposable;
  27. if (provider != null)
  28. provider.Dispose();
  29. }
  30. instances.Clear();
  31. }
  32. class CacheEntry
  33. {
  34. private Type providerType;
  35. public CacheEntry(Type providerType)
  36. {
  37. this.providerType = providerType;
  38. }
  39. public override bool Equals(object obj)
  40. {
  41. CacheEntry other = obj as CacheEntry;
  42. if (other == null) return false;
  43. return this.providerType == other.providerType;
  44. }
  45. public override int GetHashCode()
  46. {
  47. return providerType.GetHashCode();
  48. }
  49. }
  50. }
  51. }