forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionSignature.cs
More file actions
195 lines (169 loc) · 6.34 KB
/
FunctionSignature.cs
File metadata and controls
195 lines (169 loc) · 6.34 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
namespace Simple.Data
{
using System.Text;
internal sealed class FunctionSignature : IEquatable<FunctionSignature>
{
private readonly string _name;
private readonly IList<Parameter> _parameters;
private static readonly object obj = new object();
private FunctionSignature(string name, IList<Parameter> parameters)
{
_name = name;
_parameters = parameters;
}
public static string FromBinder(InvokeMemberBinder binder, object[] args)
{
if (args.Length == 1 && !(args[0] is IList))
return string.Format("{0}({1})", binder.Name, (args[0] ?? obj).GetType());
var builder = new StringBuilder(binder.Name);
builder.Append("(");
for (int i = 0; i < args.Length; i++)
{
if (binder.CallInfo.ArgumentNames.Count > i && !string.IsNullOrWhiteSpace(binder.CallInfo.ArgumentNames[i]))
{
builder.Append(binder.CallInfo.ArgumentNames[i]);
builder.Append(":");
}
var type = (args[i] ?? obj).GetType();
builder.Append(type.FullName);
var list = args[i] as IList;
if (list != null)
{
builder.AppendFormat("[{0}]", list.Count);
}
}
builder.Append(")");
return builder.ToString();
}
private static IList<Parameter> GetParameters(InvokeMemberBinder binder, object[] args)
{
var parameters = new Parameter[args.Length];
for (int i = 0; i < args.Length; i++)
{
var list = args[i] as IList;
if (list != null)
{
parameters[i] = new Parameter(NullSafeGetType(args[i]), binder.CallInfo.ArgumentNames.GetOrDefault(i), list.Count);
}
else
{
parameters[i] = new Parameter(NullSafeGetType(args[i]), binder.CallInfo.ArgumentNames.GetOrDefault(i));
}
}
return parameters;
}
private static Type NullSafeGetType(object o)
{
return o == null ? typeof (object) : o.GetType();
}
class Parameter : IEquatable<Parameter>
{
private readonly Type _type;
private readonly string _name;
private readonly int _size;
public Parameter(Type type) : this(type, null)
{
}
public Parameter(Type type, string name) : this(type, name, 0)
{
}
public Parameter(Type type, int size) : this(type, null, size)
{
}
public Parameter(Type type, string name, int size)
{
_type = type;
_name = name;
_size = size;
}
public bool Equals(Parameter other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._type, _type) && Equals(other._name, _name) && other._size == _size;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Parameter)) return false;
return Equals((Parameter) obj);
}
public override int GetHashCode()
{
unchecked
{
int result = _type.GetHashCode();
result = (result*397) ^ (_name != null ? _name.GetHashCode() : 0);
result = (result*397) ^ _size;
return result;
}
}
public static bool operator ==(Parameter left, Parameter right)
{
return Equals(left, right);
}
public static bool operator !=(Parameter left, Parameter right)
{
return !Equals(left, right);
}
}
public bool Equals(FunctionSignature other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._name, _name) && ParameterListsMatch(other._parameters, _parameters);
}
private static bool ParameterListsMatch(IList<Parameter> left, IList<Parameter> right)
{
if (left.Count != right.Count) return false;
for (int i = 0; i < left.Count; i++)
{
if (!left[i].Equals(right[i]))
{
return false;
}
}
return true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (FunctionSignature)) return false;
return Equals((FunctionSignature) obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = _name.GetHashCode()*397;
for (int i = 0; i < _parameters.Count; i++)
{
hashCode ^= _parameters[i].GetHashCode();
}
return hashCode;
}
}
public static bool operator ==(FunctionSignature left, FunctionSignature right)
{
return Equals(left, right);
}
public static bool operator !=(FunctionSignature left, FunctionSignature right)
{
return !Equals(left, right);
}
}
static class ListEx
{
public static T GetOrDefault<T>(this IList<T> list, int index)
{
return index < list.Count ? list[index] : default(T);
}
}
}