-
Notifications
You must be signed in to change notification settings - Fork 1
/
MsgDataCollection.cs
157 lines (146 loc) · 4.92 KB
/
MsgDataCollection.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
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace ThreadSupport
{
[Serializable]
public class MsgDataCollection : NameObjectCollectionBase
{
// You can add non-serializable types to the collection.
// However, the serializer with throw an exception if you try to
// serialize the collection when it contains non-serializable types.
private IEqualityComparer _keyComparer;
private SerializationInfo _collectionInfo;
public MsgDataCollection()
: this(0)
{
}
public MsgDataCollection(IEqualityComparer equalityComparer)
: this(0, equalityComparer)
{
}
public MsgDataCollection(Int32 capacity)
: base(capacity)
{
_keyComparer = StringComparer.InvariantCultureIgnoreCase;
}
public MsgDataCollection(Int32 capacity, IEqualityComparer equalityComparer)
: base(capacity, equalityComparer)
{
_keyComparer = equalityComparer ?? StringComparer.InvariantCultureIgnoreCase;
}
protected MsgDataCollection(SerializationInfo info, StreamingContext context)
: base(info, context)
{
_collectionInfo = info;
}
public Object this[Int32 index]
{
// ReSharper disable once ConvertPropertyToExpressionBody
get { return BaseGet(index); }
}
public Object this[String name]
{
get { return BaseGet(name); }
set
{
OnValidate(name, value);
BaseSet(name, value);
}
}
private Int32 IndexOfInternal(String name)
{
int keyIndex = -1;
string[] names = BaseGetAllKeys();
if (names.Length > 0)
{
for (int index = 0; index < names.Length; index++)
{
if (_keyComparer.Equals(names[index], name))
{
keyIndex = index;
break;
}
}
}
return keyIndex;
}
protected virtual void OnValidate(String name, Object value)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length == 0)
{
throw new ArgumentException("Name cannot be empty (zero length).", nameof(name));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
}
public void Add(String name, Object value)
{
OnValidate(name, value);
// Required IF 'name' MUST be unique...
if (IndexOfInternal(name) > -1)
{
throw new ArgumentException("Duplicate name.", nameof(name));
}
BaseAdd(name, value);
}
public String[] AllKeys()
{
return BaseGetAllKeys();
}
public Object[] AllValues()
{
return BaseGetAllValues(typeof(Object));
}
public void Clear()
{
BaseClear();
}
public Boolean Contains(String name)
{
return (IndexOfInternal(name) > -1);
}
public void CopyTo(Object[] array, Int32 index)
{
// Copies values to destination array.
// ICollection.CopyTo copies keys to destination array.
Object[] sourceArray = AllValues();
sourceArray.CopyTo(array, index);
}
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("keyComparer", _keyComparer, typeof(IEqualityComparer));
}
public Int32 IndexOf(string name)
{
return IndexOfInternal(name);
}
public override void OnDeserialization(Object sender)
{
base.OnDeserialization(sender);
if (_collectionInfo != null)
{
_keyComparer = _collectionInfo.GetValue("keyComparer", typeof(IEqualityComparer)) as IEqualityComparer;
_collectionInfo = null;
}
}
public void Remove(String name)
{
BaseRemove(name);
}
public void RemoveAt(Int32 index)
{
BaseRemoveAt(index);
}
}
}