PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 62 lines | 46 code | 11 blank | 5 comment | 6 complexity | 18343dfe9df442026873efeac421bf8d MD5 | raw file
 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
 7using System;
 8using System.Collections;
 9using System.Text;
10
11namespace NUnit.Core.Builders
12{
13    class ProviderCache
14    {
15        private static IDictionary instances = new Hashtable();
16
17        public static object GetInstanceOf(Type providerType)
18        {
19            CacheEntry entry = new CacheEntry(providerType);
20
21            object instance = instances[entry];
22            return instance == null
23                ? instances[entry] = Reflect.Construct(providerType)
24                : instance;
25        }
26
27        public static void Clear()
28        {
29            foreach (object key in instances.Keys)
30            {
31                IDisposable provider = instances[key] as IDisposable;
32                if (provider != null)
33                    provider.Dispose();
34            }
35
36            instances.Clear();
37        }
38
39        class CacheEntry
40        {
41            private Type providerType;
42
43            public CacheEntry(Type providerType)
44            {
45                this.providerType = providerType;
46            }
47
48            public override bool Equals(object obj)
49            {
50                CacheEntry other = obj as CacheEntry;
51                if (other == null) return false;
52
53                return this.providerType == other.providerType;
54            }
55
56            public override int GetHashCode()
57            {
58                return providerType.GetHashCode();
59            }
60        }
61    }
62}