PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/src/LinFu.Proxy/ProxyCacheEntry.cs

http://github.com/philiplaureano/LinFu
C# | 78 lines | 50 code | 15 blank | 13 comment | 21 complexity | 8d26e5ad9c789b8f96685c373c685484 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3
 4namespace LinFu.Proxy
 5{
 6    /// <summary>
 7    ///     Represents a cached proxy type.
 8    /// </summary>
 9    internal class ProxyCacheEntry
10    {
11        public Type BaseType;
12        public Type[] Interfaces;
13
14        internal ProxyCacheEntry(Type baseType, Type[] interfaces)
15        {
16            BaseType = baseType;
17            Interfaces = interfaces;
18        }
19
20
21        internal class EqualityComparer : IEqualityComparer<ProxyCacheEntry>
22        {
23            public bool Equals(ProxyCacheEntry x, ProxyCacheEntry y)
24            {
25                // Match the base t ypes
26                if (y.BaseType != x.BaseType)
27                    return false;
28
29                // If two types have the same base class and
30                // no interface, then we have a match
31                if (x.Interfaces.Length == 0 && y.Interfaces.Length == 0)
32                    return true;
33
34                // If one set of interfaces is null and the other one is not
35                // null, then there is no match
36                if (x.Interfaces == null && y.Interfaces != null ||
37                    y.Interfaces == null && x.Interfaces != null)
38                    return false;
39
40                // Initialize both interface lists and 
41                // set them up for comparison
42                var interfaceList = new HashSet<Type>();
43                var targetList = new List<Type>();
44
45                if (x.Interfaces != null && x.Interfaces.Length > 0)
46                    targetList.AddRange(x.Interfaces);
47
48                if (y.Interfaces != null)
49                    interfaceList = new HashSet<Type>(y.Interfaces);
50
51                // The length of the interfaces must match
52                if (interfaceList.Count != targetList.Count)
53                    return false;
54
55                foreach (var current in targetList)
56                    if (!interfaceList.Contains(current))
57                        return false;
58
59                return true;
60            }
61
62            public int GetHashCode(ProxyCacheEntry obj)
63            {
64                var extractor = new InterfaceExtractor();
65                var types = new HashSet<Type>(obj.Interfaces);
66                extractor.GetInterfaces(obj.BaseType, types);
67
68                // HACK: Calculate the hash code
69                // by XORing all the types together
70                var baseType = obj.BaseType;
71                var result = baseType.GetHashCode();
72                foreach (var type in types) result ^= type.GetHashCode();
73
74                return result;
75            }
76        }
77    }
78}