forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyCacheEntry.cs
84 lines (66 loc) · 2.21 KB
/
ProxyCacheEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#region Credits
// This work is based on LinFu.DynamicProxy framework (c) Philip Laureano who has donated it to NHibernate project.
// The license is the same of NHibernate license (LGPL Version 2.1, February 1999).
// The source was then modified to be the default DynamicProxy of NHibernate project.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace NHibernate.Proxy
{
public class ProxyCacheEntry : IEquatable<ProxyCacheEntry>
{
private readonly int _hashCode;
private readonly HashSet<System.Type> _uniqueInterfaces;
public ProxyCacheEntry(System.Type baseType, System.Type[] interfaces)
{
BaseType = baseType ?? throw new ArgumentNullException(nameof(baseType));
var uniqueInterfaces = new HashSet<System.Type>();
if (interfaces != null && interfaces.Length > 0)
{
uniqueInterfaces.UnionWith(interfaces.Where(i => i != null));
}
_uniqueInterfaces = uniqueInterfaces;
_hashCode = 59 ^ baseType.GetHashCode();
if (_uniqueInterfaces.Count == 0)
return;
foreach (var type in _uniqueInterfaces)
{
// This simple implementation is nonsensitive to list order. If changing it for a sensitive one,
// take care of ordering the list.
_hashCode ^= type.GetHashCode();
}
}
public System.Type BaseType { get; }
public IReadOnlyCollection<System.Type> Interfaces => _uniqueInterfaces;
public override bool Equals(object obj)
{
return Equals(obj as ProxyCacheEntry);
}
public bool Equals(ProxyCacheEntry other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(null, other) ||
// hashcode inequality allows an early exit, but their equality is not enough for guaranteeing instances equality.
_hashCode != other._hashCode ||
BaseType != other.BaseType)
{
return false;
}
return _uniqueInterfaces.SetEquals(other.Interfaces);
}
public override int GetHashCode() => _hashCode;
}
}
namespace NHibernate.Proxy.DynamicProxy
{
// Since v5.2
[Obsolete("Use NHibernate.Proxy.ProxyCacheEntry instead")]
public class ProxyCacheEntry : NHibernate.Proxy.ProxyCacheEntry
{
public ProxyCacheEntry(System.Type baseType, System.Type[] interfaces) : base(baseType, interfaces)
{
}
}
}