2using System.Collections;
3using System.Collections.Generic;
11 [ArchivingTime(nameof(ArchivingTime))]
12 public sealed
class GenericObject : ICollection<KeyValuePair<string, object>>
14 private IEnumerable<KeyValuePair<string, object>> properties =
new LinkedList<KeyValuePair<string, object>>();
15 private Dictionary<string, object> propertiesByName =
null;
16 private string collectionName =
null;
17 private string typeName =
null;
18 private int archivingTime = 0;
19 private bool propertiesUpdated =
false;
20 private Guid objectId = Guid.Empty;
64 get => this.collectionName;
65 set => this.collectionName = value;
74 set => this.typeName = value;
83 set => this.objectId = value;
92 get => this.archivingTime;
93 set => this.archivingTime = value;
101 if (this.propertiesUpdated)
102 this.BuildEnumerable();
104 return this.properties.GetEnumerator();
110 IEnumerator IEnumerable.GetEnumerator()
112 if (this.propertiesUpdated)
113 this.BuildEnumerable();
115 return this.properties.GetEnumerator();
121 public Dictionary<string,object>.KeyCollection
Keys
125 if (this.propertiesByName is
null)
126 this.BuildDictionary();
128 return this.propertiesByName.Keys;
135 public Dictionary<string, object>.ValueCollection
Values
139 if (this.propertiesByName is
null)
140 this.BuildDictionary();
142 return this.propertiesByName.Values;
151 public object this[
string PropertyName]
155 if (this.propertiesByName is
null)
156 this.BuildDictionary();
158 if (this.propertiesByName.TryGetValue(PropertyName, out
object Result))
166 if (this.propertiesByName is
null)
167 this.BuildDictionary();
169 this.propertiesByName[PropertyName] = value;
170 this.propertiesUpdated =
true;
182 if (this.propertiesByName is
null)
183 this.BuildDictionary();
185 return this.propertiesByName.TryGetValue(PropertyName, out Value);
195 if (this.propertiesByName is
null)
196 this.BuildDictionary();
198 if (!this.propertiesByName.Remove(PropertyName))
201 this.propertiesUpdated =
true;
206 private void BuildDictionary()
208 this.propertiesByName = this.properties as Dictionary<string, object>;
209 if (this.propertiesByName is
null)
211 this.propertiesByName =
new Dictionary<string, object>();
213 foreach (KeyValuePair<string, object> P
in this.properties)
214 this.propertiesByName[P.Key] = P.Value;
218 private void BuildEnumerable()
220 LinkedList<KeyValuePair<string, object>> List =
new LinkedList<KeyValuePair<string, object>>();
221 Dictionary<string, bool> Added =
new Dictionary<string, bool>();
223 foreach (KeyValuePair<string, object> P
in this.properties)
225 if (!this.propertiesByName.TryGetValue(P.Key, out
object Value))
228 List.AddLast(
new KeyValuePair<string, object>(P.Key, Value));
232 foreach (KeyValuePair<string, object> P
in this.propertiesByName)
234 if (Added.ContainsKey(P.Key))
240 this.properties = List;
250 if (this.propertiesUpdated)
251 this.BuildEnumerable();
253 return this.properties;
264 if (this.propertiesByName is
null)
265 this.BuildDictionary();
267 return this.propertiesByName.Count;
285 public void Add(KeyValuePair<string, object> item)
287 this[item.Key] = item.Value;
295 if (this.propertiesByName is
null)
296 this.propertiesByName =
new Dictionary<string, object>();
298 this.propertiesByName.Clear();
300 this.propertiesUpdated =
true;
306 public bool Contains(KeyValuePair<string, object> item)
308 if (this.propertiesByName is
null)
309 this.BuildDictionary();
311 if (!this.propertiesByName.TryGetValue(item.Key, out
object Value))
315 return item.Value is
null;
317 return Value.Equals(item.Value);
323 public void CopyTo(KeyValuePair<string, object>[] array,
int arrayIndex)
325 foreach (KeyValuePair<string, object> P
in this)
326 array[arrayIndex++] = P;
332 public bool Remove(KeyValuePair<string, object> item)
334 if (this.propertiesByName is
null)
335 this.BuildDictionary();
337 if (!this.propertiesByName.TryGetValue(item.Key, out
object Value))
342 if (!(item.Value is
null))
345 else if (!Value.Equals(item.Value))
348 this.propertiesByName.Remove(item.Key);
349 this.propertiesUpdated =
true;
358 this.collectionName != GenObj.collectionName ||
359 this.typeName != GenObj.typeName ||
360 this.objectId != GenObj.objectId)
365 if (this.propertiesByName is
null)
366 this.BuildDictionary();
368 if (GenObj.propertiesByName is
null)
369 GenObj.BuildDictionary();
371 if (this.propertiesByName.Count != GenObj.propertiesByName.Count)
374 foreach (KeyValuePair<string, object> P
in this.propertiesByName)
376 if (!GenObj.propertiesByName.TryGetValue(P.Key, out
object Value))
379 if (!PropertyEquals(Value, P.Value))
386 private static bool PropertyEquals(
object Value1,
object Value2)
388 if (Value1 is
null ^ Value2 is
null)
391 if (Value1 is
null || Value1.Equals(Value2))
394 if (Value1 is Array A && Value2 is Array B)
401 for (i = 0; i < c; i++)
403 if (!PropertyEquals(A.GetValue(i), B.GetValue(i)))
416 if (this.propertiesByName is
null)
417 this.BuildDictionary();
419 int Result = this.objectId.GetHashCode();
420 Result ^= Result << 5 ^ this.typeName.GetHashCode();
421 Result ^= Result << 5 ^ this.collectionName.GetHashCode();
423 foreach (KeyValuePair<string, object> P
in this.propertiesByName)
425 Result ^= Result << 5 ^ P.Key.GetHashCode();
426 Result ^= Result << 5 ^ P.Value.GetHashCode();
439 if (this.propertiesByName is
null)
440 this.BuildDictionary();
442 return this.propertiesByName.ContainsKey(PropertyName);
452 return this[PropertyName];
Generic object. Contains a sequence of properties.
int Count
ICollection<T>.Count
IEnumerable< KeyValuePair< string, object > > Properties
Current set of properties.
bool HasProperty(string PropertyName)
If the object has a property with a given name.
bool Remove(string PropertyName)
Removes a named property.
Dictionary< string, object >.KeyCollection Keys
Gets an enumeration of available key names.
bool TryGetFieldValue(string PropertyName, out object Value)
Gets the value of a field or property of the object, given its name.
string TypeName
Type name.
void Add(KeyValuePair< string, object > item)
ICollection<T>.Add
int ArchivingTime
Archiving time, in days. 0=No archiving. int.MaxValue=No time limit.
bool IsReadOnly
ICollection<T>.IsReadOnly
bool Contains(KeyValuePair< string, object > item)
ICollection<T>.Contains
bool Remove(KeyValuePair< string, object > item)
ICollection<T>.Remove
override int GetHashCode()
IEnumerator< KeyValuePair< string, object > > GetEnumerator()
IEnumerable<T>.GetEnumerator()
string CollectionName
Collection name.
Dictionary< string, object >.ValueCollection Values
Gets an enumeration of available values.
object GetProperty(string PropertyName)
Gets the value of a given property.
override bool Equals(object obj)
void Clear()
ICollection<T>.Clear
void CopyTo(KeyValuePair< string, object >[] array, int arrayIndex)
ICollection<T>.Contains
GenericObject(string CollectionName, string TypeName, Guid ObjectId, params KeyValuePair< string, object >[] Properties)
Generic object. Contains a sequence of properties.
GenericObject(string CollectionName, string TypeName, Guid ObjectId, IEnumerable< KeyValuePair< string, object > > Properties)
Generic object. Contains a sequence of properties.