forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicComponent.cs
215 lines (170 loc) · 5.78 KB
/
DynamicComponent.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Runtime.Serialization;
namespace NHibernate.Util
{
[Serializable]
public sealed class DynamicComponent : DynamicObject,
IDictionary,
IDictionary<string, object>,
ISerializable,
IDeserializationCallback
{
private readonly Dictionary<string, object> _data = new Dictionary<string, object>();
public DynamicComponent()
{
}
private DynamicComponent(SerializationInfo info, StreamingContext context)
{
_data = info.GetValue<Dictionary<string, object>>("Data");
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _data.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_data[binder.Name] = value;
return true;
}
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
return _data.Remove(binder.Name);
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _data.Keys;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length == 1 && indexes[0] is string key)
return _data.TryGetValue(key, out result);
return base.TryGetIndex(binder, indexes, out result);
}
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
if (indexes.Length == 1 && indexes[0] is string key)
{
_data[key] = value;
return true;
}
return base.TrySetIndex(binder, indexes, value);
}
public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes)
{
if (indexes.Length == 1 && indexes[0] is string key)
return _data.Remove(key);
return base.TryDeleteIndex(binder, indexes);
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (_data.TryGetValue(binder.Name, out var possibleDeligate) && possibleDeligate is Delegate @delegate)
{
result = @delegate.DynamicInvoke(args);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
void IDictionary.Add(object key, object value)
{
((IDictionary) _data).Add(key, value);
}
void IDictionary.Clear()
{
((IDictionary) _data).Clear();
}
bool IDictionary.Contains(object key)
{
return ((IDictionary) _data).Contains(key);
}
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return ((IDictionary) _data).GetEnumerator();
}
void IDictionary.Remove(object key)
{
((IDictionary) _data).Remove(key);
}
bool IDictionary.IsFixedSize => ((IDictionary) _data).IsFixedSize;
bool IDictionary.IsReadOnly => ((IDictionary) _data).IsReadOnly;
object IDictionary.this[object key]
{
get => ((IDictionary) _data)[key];
set => ((IDictionary) _data)[key] = value;
}
ICollection IDictionary.Keys => ((IDictionary) _data).Keys;
ICollection IDictionary.Values => ((IDictionary) _data).Values;
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable) _data).GetEnumerator();
}
void ICollection.CopyTo(Array array, int index)
{
((ICollection) _data).CopyTo(array, index);
}
int ICollection.Count => ((ICollection) _data).Count;
bool ICollection.IsSynchronized => ((ICollection) _data).IsSynchronized;
object ICollection.SyncRoot => ((ICollection) _data).SyncRoot;
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
((ICollection<KeyValuePair<string, object>>) _data).Add(item);
}
void ICollection<KeyValuePair<string, object>>.Clear()
{
((ICollection<KeyValuePair<string, object>>) _data).Clear();
}
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>) _data).Contains(item);
}
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<string, object>>) _data).CopyTo(array, arrayIndex);
}
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>) _data).Remove(item);
}
int ICollection<KeyValuePair<string, object>>.Count =>
((ICollection<KeyValuePair<string, object>>) _data).Count;
bool ICollection<KeyValuePair<string, object>>.IsReadOnly =>
((ICollection<KeyValuePair<string, object>>) _data).IsReadOnly;
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
return ((IEnumerable<KeyValuePair<string, object>>) _data).GetEnumerator();
}
void IDictionary<string, object>.Add(string key, object value)
{
((IDictionary<string, object>) _data).Add(key, value);
}
bool IDictionary<string, object>.ContainsKey(string key)
{
return ((IDictionary<string, object>) _data).ContainsKey(key);
}
bool IDictionary<string, object>.Remove(string key)
{
return ((IDictionary<string, object>) _data).Remove(key);
}
bool IDictionary<string, object>.TryGetValue(string key, out object value)
{
return ((IDictionary<string, object>) _data).TryGetValue(key, out value);
}
object IDictionary<string, object>.this[string key]
{
get => ((IDictionary<string, object>) _data)[key];
set => ((IDictionary<string, object>) _data)[key] = value;
}
ICollection<object> IDictionary<string, object>.Values => ((IDictionary<string, object>) _data).Values;
ICollection<string> IDictionary<string, object>.Keys => ((IDictionary<string, object>) _data).Keys;
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Data", _data);
}
void IDeserializationCallback.OnDeserialization(object sender)
{
_data.OnDeserialization(sender);
}
}
}