-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicRow.cs
More file actions
439 lines (376 loc) · 19.1 KB
/
Copy pathDynamicRow.cs
File metadata and controls
439 lines (376 loc) · 19.1 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;
using Binder = Microsoft.CSharp.RuntimeBinder.Binder;
// ReSharper disable ExplicitCallerInfoArgument
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMethodReturnValue.Global
// ReSharper disable RedundantNameQualifier
namespace DynamicCollection
{
// copy of DevExpress.XtraEditors.DXErrorProvider.ErrorType
public enum CellErrorType {None,Default,Information,Warning,Critical,User1,User2,User3,User4,User5,User6,User7,User8,User9}
public delegate object PropertyGetter (); // Func<object>
public delegate bool PropertySetter (object newValue); // Func<object,bool>
public delegate void PropertyChanged(object oldValue, object newValue); // Action<object,object>
public delegate void PropertyError (PropertyDescriptor propertyDescriptor, CellError cellError); // Action<PropertyDescriptor,CellError>
public class CellError
{
public CellErrorType CellErrorType ;
public string CellErrorText ;
}
// property descriptor on the row
public class PropertyDescriptor
{
// Value and Getter are mutually exclusive !!!
public object Value ; // value if no getter
public PropertyGetter Getter ; // Func<object>
public PropertySetter Setter ; // Func<object,bool>
public PropertyChanged OnChange ; // Action<object,object>
public PropertyError CheckError ; // will be used by DynamicRow descendant (DynamicRowDx) to implement IDXDataErrorInfo
public override string ToString()
{
if (Getter != null)
return Getter.Invoke().ToString() ;
return Value.ToString() ;
}
}
// Don't implement System.ComponentModel.IDataErrorInfo because current "this" implementation return Object and not string
// For IDXDataErrorInfo => see DynamicRowDx in Eortc.Buddy.Views.Extensions.DevExpressExtensions (use dynamic row callback)
//namespace System.ComponentModel
//{
// public interface IDataErrorInfo
// {
// string this[string columnName] { get; }
// string Error { get; }
// }
//}
// DynamicRow used by DynamicCollection
public class DynamicRow : DynamicObject, INotifyPropertyChanged
{
protected readonly Dictionary<string, PropertyDescriptor> DynamicProperties;
private bool _propertyChangeCallbackAllowed = true ;
// initialise row with no properties
public DynamicRow()
{
DynamicProperties = new Dictionary<string, PropertyDescriptor>();
}
// initialise row using an array of KeyValuePair
public DynamicRow(params KeyValuePair<string, object>[] propertyNames)
{
DynamicProperties = propertyNames.ToDictionary(s => s.Key, s => new PropertyDescriptor {Value = s.Value });
}
// initialise row using an IEnumerable of KeyValuePair
public DynamicRow(IEnumerable<KeyValuePair<string, object>> propertyNames) : this(propertyNames.ToArray())
{
}
// initialise row using a array of tuple key/Value
public DynamicRow(params Tuple<string, object>[] propertyNamesAndValues)
{
DynamicProperties = propertyNamesAndValues.ToDictionary(x => x.Item1, x => new PropertyDescriptor {Value = x.Item2 });
}
// initialise row using an IEnumerable of tuple key/Value
public DynamicRow(IEnumerable<Tuple<string, object>> propertyNames) : this(propertyNames.ToArray())
{
}
public override string ToString()
{
return "DynamicRow" ;
}
// Delphi style BeginUpdate. Change to properties will not fire OnPropertyChange
public void BeginUpdate()
{
_propertyChangeCallbackAllowed = false ;
}
// Delphi style EndUpdate. Subsequent properties changes will fire OnPropertyChange event
public void EndUpdate()
{
_propertyChangeCallbackAllowed = true ;
}
// Indicate if the row is on update mode (OnPropertyChange not called)
public bool IsOnUpdate()
{
return ! _propertyChangeCallbackAllowed;
}
public PropertyDescriptor DefineProperty(string propertyName)
{
return DefineProperty(propertyName, null as object); // specifying as object force to use TryAddProperty with object propertyValue
}
// Add a property (if not exist yet) using a value and an optional property change callback.
// If property already exist, the current PropertyDescriptor is returned
public PropertyDescriptor DefineProperty(
string propertyName,
object propertyValue,
PropertyChanged onPropertyChanged=null) // Action<object,string,object,object>
{
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
if (propertyDescriptor != null)
return propertyDescriptor; // row already have a propertyDescriptor, don't allow to replace it
propertyDescriptor = new PropertyDescriptor {Value = propertyValue, Getter = null, Setter = null, OnChange = onPropertyChanged} ;
DynamicProperties.Add(propertyName, propertyDescriptor);
return propertyDescriptor;
}
// Add a property (if not exist yet) using a value and a CellDescriptor with getter,setter,onchange,checkError.
// Comparable to Javascript Object.defineProperty(obj, prop, descriptor)
public PropertyDescriptor DefineProperty(
string propertyName,PropertyDescriptor propertyDescriptorParam)
{
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
if (propertyDescriptor != null)
return propertyDescriptor; // row already have a propertyDescriptor, don't allow to replace it
if (propertyDescriptorParam.Getter == null)
propertyDescriptorParam.Getter = () => null; // store null as field value
propertyDescriptor = propertyDescriptorParam;
DynamicProperties.Add(propertyName, propertyDescriptor);
return propertyDescriptor;
}
public PropertyDescriptor GetPropertyDescriptor(string propertyName)
{
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
return propertyDescriptor;
}
// Accessor
public object this[string propertyName]
{
get
{
//TTrace.Debug.Send("DynamicRow.this[ " + propertyName + "] getter") ;
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
if (propertyDescriptor == null)
return null;
if (propertyDescriptor.Getter != null)
return propertyDescriptor.Getter.Invoke() ;
return propertyDescriptor.Value ;
}
set
{
// existing property , same type
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
// Try first to find the prop in dictionnary
if (propertyDescriptor != null)// && _dynamicProperties[propertyName].GetType() == value.GetType())
{
// getter but no setter : read only prop
if (propertyDescriptor.Getter != null && propertyDescriptor.Setter == null)
return ;
var currentProp = propertyDescriptor.Getter != null ? propertyDescriptor.Getter?.Invoke() : propertyDescriptor.Value;
if (currentProp == value) // same as object.Equals(currentProp, value)
return ;
bool setterInvokeResult = true;
if (propertyDescriptor.Setter != null)
setterInvokeResult = propertyDescriptor.Setter.Invoke(value);
else
propertyDescriptor.Value = value;
if (setterInvokeResult && _propertyChangeCallbackAllowed)
{
// call onPropertyFieldChanged for the field , if defined
propertyDescriptor.OnChange?.Invoke(currentProp, value) ;
// call OnPropertyChanged on the record
OnPropertyChanged(propertyName);
}
return;
} // key exist
// slow for new property / Other type
var binder = Binder.SetMember(
CSharpBinderFlags.None,
propertyName,
value.GetType(),
new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
if (! base.TrySetMember((SetMemberBinder)binder, value))
throw new Exception("DynamicRow : Property " + propertyName + " error");
}
}
// Override DynamicObject.TrySetMember()
// Provides the implementation of setting a member.
// return true if the operation is complete, false if the call site should determine behavior.
public override bool TrySetMember(SetMemberBinder binder, object value)
{
//TTrace.Debug.Send("dynamicRow TrySetMember " + binder.Name) ;
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(binder.Name, out propertyDescriptor);
if (propertyDescriptor != null)
{
// getter but no setter : read only prop
if (propertyDescriptor.Getter != null && propertyDescriptor.Setter == null)
return true; // no error
object currentProp = propertyDescriptor.Getter != null ? propertyDescriptor.Getter.Invoke() : propertyDescriptor.Value;
if (currentProp == value) // same as object.Equals(currentProp, value)
return true; // no error
// Code differ from this[] "set" accessor : if the old value type is not the same, the new value is converted
bool setterInvokeResult = true;
if (propertyDescriptor.Setter != null)
{
setterInvokeResult = propertyDescriptor.Setter.Invoke(value);
} else if (currentProp == null || currentProp.GetType() == value.GetType()) {
// if same type, don't use converter
propertyDescriptor.Value = value;
} else {
//TTrace.Debug.Send("dynamicRow setMember convert from " + currentProp + " to " + value) ;
try
{
TypeConverter converter = TypeDescriptor.GetConverter(currentProp.GetType());
var converted = converter.ConvertFrom(value);
propertyDescriptor.Value = converted;
}
catch (FormatException /*e*/)
{
//TTrace.Error.Send($"DynamicRow.TrySetMember {binder.Name} : FormatException {e.Message}") ;
return false; // false = Error
}
catch (Exception /*e*/)
{
//TTrace.Error.Send($"DynamicRow.TrySetMember {binder.Name} : Exception {e.Message}") ;
return false; // false = error
}
}
if (setterInvokeResult && _propertyChangeCallbackAllowed)
{
// call onPropertyFieldChanged for the field , if defined
propertyDescriptor.OnChange?.Invoke(currentProp, value) ;
// call OnPropertyChanged on the record
OnPropertyChanged(binder.Name);
}
return true; // no error
} // key exist
return base.TrySetMember(binder, value);
}
// Override DynamicObject.TryGetMember()
// Provides the implementation of getting a member.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
//TTrace.Debug.Send("dynamicRow TryGetMember " + binder.Name) ;
PropertyDescriptor propertyDescriptor;
DynamicProperties.TryGetValue(binder.Name, out propertyDescriptor);
if (propertyDescriptor != null)
{
result = propertyDescriptor.Getter != null ? propertyDescriptor.Getter.Invoke() : propertyDescriptor.Value;
return true;
}
//TTrace.Debug.Send("dynamicRow TryGetMember not found for " + binder.Name) ;
return base.TryGetMember(binder, out result);
}
// override DynamicObject.GetDynamicMemberNames : Returns the enumeration of all dynamic member names.
public override IEnumerable<string> GetDynamicMemberNames()
{
return DynamicProperties.Keys.ToArray();
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private /* protected virtual*/ void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
public void RaisePropertyChange(string propertyName)
{
if (_propertyChangeCallbackAllowed)
{
//PropertyDescriptor propertyDescriptor;
//DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
//if (propertyDescriptor != null)
//{
// // call onPropertyFieldChanged for the field , if defined
// propertyDescriptor.OnChange?.Invoke(propertyDescriptor.Value, propertyDescriptor.Value) ;
//}
// call OnPropertyChanged on the record, catched by devexpress to refresh data
OnPropertyChanged(propertyName);
}
}
//using System.Linq.Expressions;
//using System.Reflection;
//public bool TryGetProperty(string propertyName, out object propertyValue)
//{
// PropertyDescriptor propertyDescriptor;
// DynamicProperties.TryGetValue(propertyName, out propertyDescriptor);
// if (propertyDescriptor != null)
// {
// propertyValue = propertyDescriptor.Getter != null ? propertyDescriptor.Getter.Invoke() : propertyDescriptor.Value;
// return true;
// }
// propertyValue = null;
// return false;
//}
//public IEnumerable<String> GetDynamicValues()
//{
// return DynamicProperties.Values.Select(propDescriptor => propDescriptor.ToString()).ToArray(); // this will call PropertyDescriptor.ToString that check for Getter
//}
//private string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
//{
// if (propertyExpression == null)
// throw new ArgumentNullException("propertyExpression");
// MemberExpression body = propertyExpression.Body as MemberExpression;
// if (body == null)
// throw new ArgumentException(@"Invalid argument", "propertyExpression");
// PropertyInfo property = body.Member as PropertyInfo;
// if (property == null)
// throw new ArgumentException(@"Argument is not a property", "propertyExpression");
// return property.Name;
//}
//protected bool Set<T>(Expression<Func<T>> selectorExpression, ref T field, T newValue)
//{
// if (EqualityComparer<T>.Default.Equals(field, newValue))
// return false;
// field = newValue;
// string propertyName = GetPropertyName(selectorExpression);
// // TO DO : call onPropertyFieldChanged for the field (this, propertyName , currentProp , value)
// // ...
// // call OnPropertyChanged on the record
// OnPropertyChanged(propertyName);
// return true;
//}
//protected bool Set<T>(string propertyName, ref T field, T newValue)
//{
// if (EqualityComparer<T>.Default.Equals(field, newValue))
// return false;
// field = newValue;
// // TO DO : call onPropertyFieldChanged for the field (this, propertyName , currentProp , value)
// // ...
// // call OnPropertyChanged on the record
// OnPropertyChanged(propertyName);
// return true;
//}
}
public class DynamicRow<TEntity> : DynamicRow where TEntity : class
{
public TEntity Entity {get;}
public DynamicRow() //: base()
{
}
public DynamicRow(TEntity entity) //: base()
{
Entity = entity ;
}
public DynamicRow(params KeyValuePair<string, object>[] propertyNames) : base(propertyNames) { }
public DynamicRow(IEnumerable<KeyValuePair<string, object>> propertyNames) : base(propertyNames.ToArray()) { }
public DynamicRow(params Tuple<string, object>[] propertyNamesAndValues) : base (propertyNamesAndValues) { }
public DynamicRow(IEnumerable<Tuple<string, object>> propertyNames) : base(propertyNames.ToArray()) { }
public DynamicRow(TEntity entity, params KeyValuePair<string, object>[] propertyNames) : base(propertyNames) {Entity = entity ;}
public DynamicRow(TEntity entity, IEnumerable<KeyValuePair<string, object>> propertyNames) : base(propertyNames.ToArray()) {Entity = entity ;}
public DynamicRow(TEntity entity, params Tuple<string, object>[] propertyNamesAndValues) : base (propertyNamesAndValues) {Entity = entity ;}
public DynamicRow(TEntity entity, IEnumerable<Tuple<string, object>> propertyNames) : base(propertyNames.ToArray()) {Entity = entity ;}
public override string ToString()
{
return "DynamicRow <" + Entity?.GetType() + "> " + Entity ;
}
}
public class DynamicNode: DynamicRow<Object>
{
public DynamicNode(object entity) : base(entity){}
public DynamicRow<Object> Parent {get;set;}
public DynamicCollection<Object> Children {get;set;}
public override string ToString()
{
return "DynamicNode <" + Entity?.GetType() + "> " + Entity ;
}
}
}